Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter GetIt Plugin - No type xxx is registered inside GetIt

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;
}
like image 245
Marco Papula Avatar asked Jan 25 '23 04:01

Marco Papula


1 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());
}
like image 114
Loïc Fonkam Avatar answered Feb 02 '23 21:02

Loïc Fonkam