Skip to content

Can't register AutoRoute v6 with GetIt #346

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

Closed
lukeirvin opened this issue Oct 17, 2023 · 2 comments
Closed

Can't register AutoRoute v6 with GetIt #346

lukeirvin opened this issue Oct 17, 2023 · 2 comments

Comments

@lukeirvin
Copy link

I've just updated to the latest version of auto_route & get_it but I am getting a register error.

Below I've provided examples of my setup.

Here are my versions:
Flutter - 3.13.7
Dart - 3.1.3
Xcode - 15.0
CocoaPods - 1.13.0
auto_route: ^7.8.4
get_it: ^7.6.4
auto_route_generator: ^7.0.0
build_runner: ^2.4.6

Error:

Bad state: GetIt: Object/factory with type $AppRouter is not registered inside GetIt. 
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?)

getit.dart:

import 'package:get_it/get_it.dart';
import 'router.gr.dart';

GetIt getIt = GetIt.instance;

void setup() {
  getIt.registerSingleton($AppRouter);
}

router.dart:

import 'package:auto_route/auto_route.dart';
import 'router.gr.dart';

@AutoRouterConfig()
class AppRouter extends $AppRouter {
  @override
  List<AutoRoute> get routes => [
      // my routes
   ];
}

app.dart:

import 'package:auto_route/auto_route.dart';
import 'getit.dart';
import 'router.gr.dart';

 @override
  Widget build(BuildContext context) {
    final appRouter = getIt<$AppRouter>();

    return MaterialApp.router(
            routerConfig: appRouter.config(),
            routerDelegate: appRouter.delegate(
              navigatorObservers: () => [
                AutoRouteObserver(),
              ],
            ),
            routeInformationParser: appRouter.defaultRouteParser(),
            builder: (BuildContext context, Widget? child) {
              // return...
            },
        );
}

main.dart:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'app.dart';
import 'getit.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  setup();
  runApp(const MyApp());
}
@escamoteur
Copy link
Collaborator

escamoteur commented Oct 18, 2023 via email

@lukeirvin
Copy link
Author

I was able to resolve this. Closing this issue out.

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