In Flutter auto_route package, what type do I need to cast the builder as?

Viewed 2586

I recently upgraded Flutter to 2.0 and migrated to null safety.

My code is as follows

...
import 'package:auto_route/auto_route.dart';
import 'package:metv/presentation/routes/router.gr.dart' as app_router;

child: MaterialApp(
            title: 'Notes',
            themeMode: darkMode ? ThemeMode.dark : ThemeMode.light,
            theme: Themes().themeLight,
            darkTheme: Themes().themeDark,
            debugShowCheckedModeBanner: false,
            builder: ExtendedNavigator.builder<app_router.Router>(
              router: app_router.Router(),
            ),
          ),

I am now getting the following error message in the <app_router.Router> line of code.

'Router' doesn't extend 'RouterBase'. Try using a type that is or is a subclass of 'RouterBase'.

I am not sure what needs to replace the broken code. Any help would be appreciated.

Thanks

2 Answers

Figured the answer out. I changed the above code to:

import 'package:metv/presentation/routes/router.gr.dart' as app_router;
...
final _appRouter = app_router.Router();
...
child: MaterialApp(
            title: 'Notes',
            themeMode: darkMode ? ThemeMode.dark : ThemeMode.light,
            theme: Themes().themeLight,
            darkTheme: Themes().themeDark,
            debugShowCheckedModeBanner: false,
            home: MaterialApp.router(
              routerDelegate: _appRouter.delegate(),
              routeInformationParser: _appRouter.defaultRouteParser(),
            ),
          ),

Even though there is already right answer published still it might be useful for extra clarification. auto_route 2.2.0 has some breaking changes. Instead of doing

return MaterialApp
      builder: ExtendedNavigator<Router>(
      key: _exNavigatorKey,
      router: Router(),
    ),

You need to do

final router = AppRouter()  

  return MaterialApp(
  home: MaterialApp.router(
      routeInformationParser: router.defaultRouteParser(),
      routerDelegate: router.delegate()),
);

AppRouter is the autogenerated class. You define the name yourself.

@MaterialAutoRouter(  
 replaceInRouteName: 'Page,Route',  
 routes: <AutoRoute>[  
  AutoRoute(page: BookListPage, initial: true),  
  AutoRoute(page: BookDetailsPage),  
   ],  
  )  
class $AppRouter {}

So make sure you use exactly the same generated class.

The ExtendedNavigator class is not available anymore. As there are other breaking changes as well I would suggest to go through the documentation once more.

Related