Flutter GetIt Plugin - No type xxx is registered inside GetIt

Viewed 14685

I set everything up as shown in the example project:

import 'package:get_it/get_it.dart';
import 'package:places/services/authService.dart';

final locator = GetIt.instance;

void setupLocator() {
  locator.registerSingleton<AuthService>(AuthService());
  print("registered");
}

with the call in the main file

void main() {
  setupLocator();
  runApp(MyApp());
}

I have some Check where the locator also correctly return my AuthService

class AuthGuardView extends StatefulWidget {
  AuthGuardView({Key key}) : super(key: key);

  @override
  _AuthGuardViewState createState() => _AuthGuardViewState();
}

class _AuthGuardViewState extends State<AuthGuardView> {
  @override
  Widget build(BuildContext context) {
    return ViewModelProvider<AuthGuardViewModel>.withConsumer(
      viewModel: AuthGuardViewModel(),
      onModelReady: (model) => model.initialise(),
      builder: (context, model, child) => model.isLoggedIn
          ? Container(
              color: Colors.white,
              child: Text("Logged In"),
            )
          : SignUpView(),
    );
  }
}


class AuthGuardViewModel extends ChangeNotifier {
  AuthService _authService = locator<AuthService>();
  bool isLoggedIn = false;

  void initialise() async {
    isLoggedIn = await _authService.isLoggedIn();
    notifyListeners();
  }
}

If I do the exact same thing inside the ViewModel for the SignUpView I get the following error

flutter: The following assertion was thrown building SignUpView(dirty, state: _SignUpViewState#01129):
flutter: No type AuthService is registered inside GetIt.
flutter:  Did you forget to pass an instance name?
flutter: (Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;did you
flutter: forget to register it?)
flutter: 'package:get_it/get_it_impl.dart':
flutter: Failed assertion: line 248 pos 14: 'instanceFactory != null'

In the ViewModel for the AuthGuard I do successfully retrieve the auth service. I also commented out the locator code (because I thought it might be the async call or something like that) but the same error persists.

I am using get_it: ^4.0.1 but the error persists when downgrading to 3.x.x

enter image description here

Here the SignUpViewModel

class SignUpViewModel extends ChangeNotifier {
  SignUpViewModel(){
    if(locator.isRegistered<AuthService>()) {
      AuthService _authService = locator<AuthService>();
    } 
  }
  var textInputFormatter = [
    WhitelistingTextInputFormatter(RegExp(r'\d')),
    PhoneNumberTextInputFormatter()
  ];
  var textEditingController;
  var context;
}
4 Answers

This happens when the class to be registered as singleton has async methods. To fix this you need to await the singleton to be fully generated before runApp() is ran.

void main() async {

/*  WidgetsFlutterBinding.ensureInitialized() is required in Flutter v1.9.4+ 
 *  before using any plugins if the code is executed before runApp. 
 */
  WidgetsFlutterBinding.ensureInitialized();


// Configure injecction
   await setupLocator();

   runApp(MyApp());
}

Adding this answer, as I think it might help others!

I have faced the same issue earlier. For me, it was due to an ordering issue. So make sure to initiate/declare the dependency objects first and then instantiate/declare the dependent one.

Using the latest get_it version in pubspec.yaml ( now it is get_it: ^4.0.2 ) resolve the issue for me.

I have also faced this issue. Nothing made sense. Then I remembered, that I recently did case sensitive renaming.

I changed i.e. Services/Database/Database.dart to services/database/database.dart, but in one file, I used import with the lowercased version, while in the other, it still was the uppercased version. Making the case consistent throughout the project was the fix I needed.

Related