skip to Main Content

How can I pass values from main to the initial route of the app? I want to get the data from a sqflite database. I would prefer to make the initialization inside main function rather than the initial route and I am trying to figure out how to set the routes

main.dart

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await configureLocalTimeZone();
  getIt.registerLazySingleton<UserRepositories>(() => UserRepositories());
  getIt.registerSingleton<AppRouter>(AppRouter());
  initializeNotifications();
  DatabaseService databaseService = DatabaseService.instance;
  final alarms = await databaseService.getAlarms();
  AppRouter router = AppRouter();
  runApp(MyApp(appRouter: router));
}

class MyApp extends StatelessWidget {
  
  final AppRouter appRouter;
  MyApp({Key? key, required this.appRouter}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(providers: [
      BlocProvider<UserBloc>(
        create: (_) => UserBloc()..add(FetchUser()), // Add FetchUser event here
      ),
      BlocProvider<LoginBloc>(
        create: (_) => LoginBloc(userRepository: getIt<UserRepositories>()), // Add FetchUser event here
      ),
    ],
        child: MaterialApp.router(
          routerConfig: appRouter.config(),
        )
    );
  }
}

AppRouter.dart

import 'package:auto_route/auto_route.dart';

import 'app_router.gr.dart';

@AutoRouterConfig(replaceInRouteName: 'Screen,Route')
class AppRouter extends $AppRouter {


  @override
  List<AutoRoute> get routes => [
    AutoRoute(page: MainRoute.page, initial: true), // I WANT TO PASS ALARMS AND SERVICE TO MAIN SCREEN
  ];
}

2

Answers


  1. Update the MainRoute.page to accept the required data, such as alarms and databaseService.

    class MainRoutePage extends StatelessWidget {
      final List<Alarm> alarms;
      final DatabaseService databaseService;
    
      MainRoutePage({
        Key? key, 
        required this.alarms, 
        required this.databaseService
      }) : super(key: key);
      
      ...
    }
    

    Modify your AppRouter to accept these values and pass them to the MainRoute.page.

    class AppRouter extends $AppRouter {
      final List<Alarm> alarms;
      final DatabaseService databaseService;
    
      AppRouter({
        required this.alarms,
        required this.databaseService,
      });
    
      @override
      List<AutoRoute> get routes => [
        AutoRoute(
          page: MainRoute.page, 
          initial: true, 
          args: [alarms, databaseService]
        ),
      ];
    }
    

    In your main function, pass the alarms and databaseService to the AppRouter when you’re instantiating it.

    AppRouter router = AppRouter(alarms: alarms, databaseService: databaseService);
    

    Now, when the MainRoute.page is created, it will receive both the alarms and databaseService as arguments.

    Login or Signup to reply.
  2. AutoRoute automatically detects and handles your page arguments for you, the generated route object will deliver all the arguments your page needs including path/query params.

    e.g. The following page widget will take an argument of type Book.

    @RoutePage()      
    class BookDetailsPage extends StatelessWidget {                    
     const BookDetailsPage({required this.book});                    
                        
      final Book book;                 
      ...               
    
     
    

    Note: Default values are respected. Required fields are also respected and handled properly.

    The generated BookDetailsRoute will deliver the same arguments to its corresponding page.

    router.push(BookDetailsRoute(book: book));  
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search