fluttercommunity / get_it

Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur
https://pub.dev/packages/get_it
MIT License
1.36k stars 149 forks source link

Can't register AutoRoute v6 with GetIt #346

Closed lukeirvin closed 1 year ago

lukeirvin commented 1 year ago

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 commented 1 year ago

And that worked with the previous versions? What made it break, the version change of autoroute or get_it? Am 18. Okt. 2023, 00:56 +0200 schrieb lukeirvin @.***>:

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 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 main() async { WidgetsFlutterBinding.ensureInitialized(); setup(); runApp(const MyApp()); } — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

lukeirvin commented 1 year ago

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