vrouter icon indicating copy to clipboard operation
vrouter copied to clipboard

History State is null

Open daichehadeo opened this issue 4 years ago • 17 comments

Hello,

I have an issue in the :

static Map<String, dynamic> _getHistoryState() {
    var globalState = html.window.history.state;

    // If no history state has ever been created and
....

  }

The var globalState = html.window.history.state; is always null and this throw this exception : throw Exception( 'Tried to get applicationInstanceId but it has not been set');

I was only able to get this stacktrace nothing more:

errors.dart:202 Uncaught Error: Unexpected null value.
    at Object.throw_ [as throw] (errors.dart:251)
    at Object.nullCheck (operations.dart:528)
    at _engine._PointerAdapter.new.[_getSanitizer] (pointer_binding.dart:490)
    at pointer_binding.dart:551
    at pointer_binding.dart:508
    at loggedHandler (pointer_binding.dart:216)
    at Function._getHistoryState (browser_helpers_web.dart:204)
    at Function.getHistoryIndex (browser_helpers_web.dart:166)
    at Function._getInitialHistoryIndex (vurl_history_web.dart:97)
    at new vurl_history_web.VHistoryWeb.new (vurl_history_web.dart:22)
    at Function.implementation (vurl_history.dart:17)
    at new vrouter_scope._VRouterScopeState.new (vrouter_scope.dart:67)
    at vrouter_scope.VRouterScope.new.createState (vrouter_scope.dart:48)
    at new framework.StatefulElement.new (framework.dart:4754)
    at vrouter_scope.VRouterScope.new.createElement (framework.dart:737)
    at framework.StatefulElement.new.inflateWidget (framework.dart:3668)

My use case is a redirection after an authentication and it failed in the VGuard widget. Do you have an idea why ?

daichehadeo avatar Nov 29 '21 13:11 daichehadeo

I have some ideas and you are not the first to have this issue. However so far I have not been able to reproduce the issue on my setup. Maybe you could provider me with a small reproducible example?

lulupointu avatar Nov 29 '21 13:11 lulupointu

I will try to do it but for now I m enable to have some breakpoints or more logs to understand where exactly the problem is so it's hard to create an example with this. But If I found how I will do it.

daichehadeo avatar Nov 29 '21 13:11 daichehadeo

The lines you pointed to in your first comment seem to be the logical place where the problem would be. The biggest issue for me is that it's not triggered under normal circumstances since I cannot reproduce it, therefore it's hard for my to fix it :/

lulupointu avatar Nov 29 '21 13:11 lulupointu

Yees I understand, I continue to investigate and keep you in touch if I have more informations. If I send you all the stacktrace form the beginning will it be use full for you ?

daichehadeo avatar Nov 29 '21 13:11 daichehadeo

I don't think so, I've already seen them in an other issue but they weren't sufficient for me to understand what was the issue.

lulupointu avatar Nov 29 '21 13:11 lulupointu

Ok more infos: In my case it throw the exception 4 times and then it disappear but the real issue for me was a wrong context given to the VGuard widget so it was looping infinitely :

VGuard(
                beforeEnter: (redirector) => _process(
                  context, // correct state
                  //navigatorKey.currentState?.overlay?.context, => Wrong state
                  redirector,
                ),
                stackedRoutes: [
                  VWidget(
                    path: '/callback',
                    widget: const SizedBox(),
                  ),
                ],
              ),

daichehadeo avatar Nov 29 '21 14:11 daichehadeo

How do you get the context which gives you the "correct state" ?

lulupointu avatar Nov 29 '21 14:11 lulupointu

I had to use a Builder widget:

Builder(
          builder: (context) => VRouter(
            mode: VRouterMode.history,

daichehadeo avatar Nov 29 '21 14:11 daichehadeo

And what do you use the context for ? Getting Providers which are above VRouter ?

lulupointu avatar Nov 29 '21 14:11 lulupointu

Yes exactly

daichehadeo avatar Nov 29 '21 14:11 daichehadeo

Is your VGuard triggered when the app launches or when it's already launched and let's say you click on a button ?

lulupointu avatar Nov 29 '21 14:11 lulupointu

The scenario is:

  • I have a VGuard to test if I m logged in.
  • If not I go to my login page which is an external url from my application.
  • I log in and the external website redirect to this /callback url with a VGuard so the application is already running but not "in front" if we can say.

daichehadeo avatar Nov 29 '21 14:11 daichehadeo

I see, this makes sense.

I would have to look into VRouter source code but I might be able to fix this. Thanks for your time :)

(Btw the Builder approach is much cleaner is my opinion ;) )

lulupointu avatar Nov 29 '21 14:11 lulupointu

Your welcome!

daichehadeo avatar Nov 29 '21 14:11 daichehadeo

This should be fixed in 1.2.0+15. Maybe will you have an other error but hopefully a better one.

Please let me know if you got 5 minutes to check it out :blush:

lulupointu avatar Dec 01 '21 11:12 lulupointu

I did a try and here are the 4 errors:

Error: Exception: Tried to get applicationInstanceId but it has not been set

then: Error: Exception: Tried to get historyLength but it has not been set

then: Error: Exception: Tried to get appHistoryState but it has not been set

then: `Error: Error: Could not find VRouterScope above this VRouterScope Widget.

If you are using ...App.router with VRouterDelegate, make sure that you wrap ...App.router in VRouterScope.

If you are using VRouter, CupertinoVRouter or WidgetsVRouter directly, this is a bug, please fill an issue at https://github.com/lulupointu/vrouter/issues.``

Here is my configuration:

VRouter(
            mode: VRouterMode.history,
            theme: Theme.of(context),
            navigatorKey: navigatorKey,
            routes: [
// Routes
]);

daichehadeo avatar Dec 02 '21 08:12 daichehadeo

Hi, sorry for coming back to this only now. Could you share a reproducible piece of code? Because I don't see why you still have the issue.

lulupointu avatar Feb 02 '22 21:02 lulupointu

Closing since I can't help without a concrete example of the last issue

lulupointu avatar Apr 28 '23 18:04 lulupointu