jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.32k stars 1.62k forks source link

When using parameters on initialRoute, the widget crashes #75

Closed jlubeck closed 4 years ago

jlubeck commented 4 years ago

When I initialized my GetMaterialApp like this:

  Widget build(BuildContext context) {
    Get.config(enableLog: false);
    return GetMaterialApp(
      initialRoute: '/qr/84226ff6-6ded-4b9d-88c9-95a210896d3a',
      namedRoutes: { '/qr/:code': GetRoute(
        page: QRScreen(),
      ),
    },
      routingCallback: MiddleWare.observer,
    );
  }

And I start the app, I get this error:

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown building IconTheme(color: Color(0xdd000000)):
flutter: The getter 'route' was called on null.
flutter: Receiver: null
flutter: Tried calling: route
flutter:
flutter: The relevant error-causing widget was:
flutter:   MaterialApp
flutter:   file:///Users/jan/.pub-cache/hosted/pub.dartlang.org/get-2.0.10/lib/src/root/root_widget.dart:172:12
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
flutter: #1      _GetMaterialAppState.namedRoutesGenerate (package:get/src/root/root_widget.dart:132:40)
flutter: #2      _WidgetsAppState._onGenerateRoute (package:flutter/src/widgets/app.dart:999:36)
flutter: #3      NavigatorState._routeNamed (package:flutter/src/widgets/navigator.dart:3138:44)
flutter: #4      Navigator.defaultGenerateInitialRoutes (package:flutter/src/widgets/navigator.dart:2162:32)
flutter: #5      NavigatorState.initState (package:flutter/src/widgets/navigator.dart:2582:39)
flutter: #6      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4640:58)
flutter: #7      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
flutter: ...     Normal element mounting (216 frames)
flutter: #223    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
flutter: #224    Element.updateChild (package:flutter/src/widgets/framework.dart:3214:18)
flutter: #225    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
flutter: #226    _InheritedProviderScopeMixin.performRebuild (package:provider/src/inherited_provider.dart:221:11)
flutter: #227    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
flutter: #228    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4481:5)
flutter: #229    ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
flutter: ...     Normal element mounting (7 frames)
flutter: #236    SingleChildWidgetElementMixin.mount (package:nested/nested.dart:223:11)
flutter: #237    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
flutter: #238    Element.updateChild (package:flutter/src/widgets/framework.dart:3214:18)
flutter: #239    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1148:16)
flutter: #240    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1119:5)
flutter: #241    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1061:17)
flutter: #242    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2607:19)
flutter: #243    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1060:13)
flutter: #244    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:941:7)
flutter: #245    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:922:7)
flutter: (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
flutter:
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════

I was able to fix it locally by changing this on the file root_widget.dart

before:

    /// onGenerateRoute to FlutterWeb is Broken on Dev/Master. This is a temporary
    /// workaround until they fix it, because the problem is with the 'Flutter engine',
    /// which changes the initial route for an empty String, not the main Flutter,
    /// so only Team can fix it.
    final parsedString = parse.split(
        settings.name == '' ? (widget.initialRoute ?? '/') : settings.name);

    String settingsname = parsedString.route;

after:

    /// onGenerateRoute to FlutterWeb is Broken on Dev/Master. This is a temporary
    /// workaround until they fix it, because the problem is with the 'Flutter engine',
    /// which changes the initial route for an empty String, not the main Flutter,
    /// so only Team can fix it.
    var parsedString = parse.split(
        settings.name == '' ? (widget.initialRoute ?? '/') : settings.name);

    if(parsedString == null) {
      parsedString = AppRouteMatch();
      parsedString.route = settings.name;
    }

    String settingsname = parsedString.route;

Not making a pull request as I'm not sure this break something else, or what. But I figured I would give you what seems to work for me.

Thank you

jonataslaw commented 4 years ago

They just made updates to the master with changes to onGenerateRoute that may have an impact on Get, I will take advantage and include this in the tests, thanks for the suggestion.

jlubeck commented 4 years ago

Nice, I'm on beta by the way, not master

jlubeck commented 4 years ago

Flutter (Channel beta, v1.17.0-3.4.pre, on Mac OS X 10.15.4 19E287, locale en-US)

jonataslaw commented 4 years ago

Yes, I just commented because I have the Get code open doing tests for the master, and I will include that in the tests as well.

jonataslaw commented 4 years ago

Apparently, at least on the master, the Flutter forces the initial route to be '/'. When using any other named route, such as '/ home'. is displayed in the log [GOING TO ROUTE] /home and then [GOING TO ROUTE] /. Can you confirm that the same is true in the beta? Because if this is a standard behavior, your solution will cause the initial routes to be duplicated. Right after navigating to your route, I also notice that it's current route is replaced by null. If the first behavior is observed in the beta, I believe that there is nothing that can be done besides opening an issue in Flutter (I would have to reconstruct all materialApp to fix this error, and changing the MaterialApp would make the library unstable). In case you have not noticed this, I will downgrade to beta version to apply the patch.

jonataslaw commented 4 years ago

Fix on 2.1.0. Thank you so much for this. I still have things to investigate, but about the Flutter part, and why it clears the initial route on the master. And it forces the use of '/', but this is already about the Flutter.