Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto_route compatibility #103

Closed
MasterHiei opened this issue Jan 25, 2021 · 9 comments
Closed

auto_route compatibility #103

MasterHiei opened this issue Jan 25, 2021 · 9 comments

Comments

@MasterHiei
Copy link

MasterHiei commented Jan 25, 2021

Describe the bug

The home property of MaterialApp is unnecessary when use auto_route package.
But if I use this package together without the home property, build will be failed because assertion failure.

To Reproduce
Steps to reproduce the behavior:

  1. Add auto_route: ^0.6.9 and bot_toast: ^3.0.5 to pubspec.yaml
  2. Run flutter pub get
  3. Create MaterialApp with auto_route and bot_toast like below
    final botToastBuilder = BotToastInit();

    return MaterialApp(
      navigatorObservers: [BotToastNavigatorObserver()],
      builder: (context, child) {
        final navigatorBuilder = ExtendedNavigator.builder(
          initialRoute: '/',
          router: Router(),
        );
        return botToastBuilder(context, navigatorBuilder(context, child));
      },
      // home: Container(), // must add
      // ...
    );
  1. Start application and see error

Expected behavior

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building MaterialApp(dirty, state: _MaterialAppState#3df4f):
If no route is provided using home, routes, onGenerateRoute, or onUnknownRoute, a non-null callback for the builder property must be provided, and the other navigator-related properties, navigatorKey, initialRoute, and navigatorObservers, must have their initial values (null, null, and the empty list, respectively).
'package:flutter/src/widgets/app.dart':
Failed assertion: line 230 pos 10: '(home != null ||
          routes.isNotEmpty ||
          onGenerateRoute != null ||
          onUnknownRoute != null)
         ||
         (builder != null &&
          navigatorKey == null &&
          initialRoute == null &&
@MMMzq
Copy link
Owner

MMMzq commented Jan 26, 2021

This doesn't seem to be an error caused by bot_toast, but an assertion error caused by you not meeting the MaterialApp parameter passing requirements. You can see if the necessary parameters are missing, or you can try to remove botToastBuilder to see if it still reports an error.

@MasterHiei
Copy link
Author

Yes, everything is fine before botToastBuilder append.

return MaterialApp(
      builder: ExtendedNavigator.builder(
        initialRoute: '/',
        router: Router(),
      ),
      // ...
    );

The assertion error occurred on nested builder block only.

@MMMzq
Copy link
Owner

MMMzq commented Jan 26, 2021

What is your flutter version?

@MasterHiei
Copy link
Author

Flutter 1.22.5

@MMMzq
Copy link
Owner

MMMzq commented Jan 26, 2021

I used the following code, did not use bot_toast at all, and reported the same error.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      builder: (context, child) {
        return child;
      },
    );
  }
}

WidgetsApp:

       assert(
         (home != null ||
          routes.isNotEmpty ||
          onGenerateRoute != null ||
          onUnknownRoute != null)
         ||
         (builder != null &&
          navigatorKey == null &&
          initialRoute == null &&
          navigatorObservers.isEmpty),
         'If no route is provided using '
         'home, routes, onGenerateRoute, or onUnknownRoute, '
         'a non-null callback for the builder property must be provided, '
         'and the other navigator-related properties, '
         'navigatorKey, initialRoute, and navigatorObservers, '
         'must have their initial values '
         '(null, null, and the empty list, respectively).'
       )

@MMMzq
Copy link
Owner

MMMzq commented Jan 26, 2021

This error is caused by the use of BotToastNavigatorObserver(), which causes the condition of navigatorObservers.isEmpty to be invalid

builder != null &&
navigatorKey == null &&
initialRoute == null &&
navigatorObservers.isEmpty

@MasterHiei
Copy link
Author

Yes, it is. But in this case, I used auto_route package to complete the routing system of my app.

I declared some routes like below in router.dart file, and an implementation file router.gr.dart will be generated after running flutter pub run build_runner build.

It means the HomePage is the first page of my app.

import 'package:auto_route/auto_route.dart';
import 'package:auto_route/auto_route_annotations.dart';

import 'home_page.dart';

@AdaptiveAutoRouter(
  routes: <AutoRoute>[
    AdaptiveRoute<void>(
      initial: true,
      name: 'home',
      page: HomePage,
    ),
  ],
)
class $Router {}
class Routes {
  static const String home = '/';
  static const all = <String>{
    home,
  };
}

class Router extends RouterBase {
  @override
  List<RouteDef> get routes => _routes;
  final _routes = <RouteDef>[
    RouteDef(Routes.home, page: HomePage),
  ];
  @override
  Map<Type, AutoRouteFactory> get pagesMap => _pagesMap;
  final _pagesMap = <Type, AutoRouteFactory>{
    HomePage: (data) {
      return buildAdaptivePageRoute<void>(
        builder: (context) => HomePage(),
        settings: data,
      );
    },
  };
}

Then I add the builder function of auto_route as MaterialApp builder without any additional properties such as navigatorObservers and home.

return MaterialApp(
      builder: ExtendedNavigator.builder(
        initialRoute: '/',
        router: Router(),
      ),
      title: 'Title',
      // ...
    );

That works for me.
But if I append bot_toast builder function, assertion error will occur.

@MMMzq
Copy link
Owner

MMMzq commented Jan 26, 2021

The code works fine.

    final botToastBuilder = BotToastInit();
    return MaterialApp(
      builder: (context, child) {
        final navigatorBuilder = ExtendedNavigator.builder(
          observers:  [BotToastNavigatorObserver()],
          router: Router(),
        );
        return botToastBuilder(context, navigatorBuilder(context, child));
      },
      title: 'Title',
    );

See Milad-Akarie/auto_route_library#154

@MasterHiei
Copy link
Author

Oh! Everything worked! Thank you for your response 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants