Skip to content

Invalid argument (Object of type RegisterModel is not registered inside GetIt. #37

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
MuhammamdArslanKhan opened this issue Jan 4, 2020 · 3 comments

Comments

@MuhammamdArslanKhan
Copy link

MuhammamdArslanKhan commented Jan 4, 2020

I have registered my model but it still says "RegisterModel is not registered inside GetIt"

Here is my View class

class RegistrationView extends StatefulWidget {

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

class _RegistrationViewState extends State<RegistrationView> {
  
  @override
  Widget build(BuildContext context) {

    return BaseView<RegisterModel>(
        builder: (context, model, child) => Scaffold(
          
          body:Container()
      ),
    );
    // TODO: implement build
  }
}

Here is View Model

class RegisterModel extends BaseModel {
  final AuthenticationService _authenticationService = locator<AuthenticationService>();

  String errorMessage;

  Future<bool> register(String userIdText) async {
    setState(ViewState.Busy);
    var userId = int.tryParse(userIdText);

    // Not a number
    if(userId == null) {
      errorMessage = 'Value entered is not a number';
      setState(ViewState.Idle);
      return false;
    }

    var success = await _authenticationService.register(userId);

    // Handle potential error here too.

    setState(ViewState.Idle);
    return success;
  }
}

Here is locator

GetIt locator = GetIt.instance;

void setupLocator() {

  locator.registerLazySingleton(() => AuthenticationService());
  locator.registerLazySingleton(() => Api());
  locator.registerFactory(() => RegisterModel());

}```
My BaseModel class

class BaseModel extends ChangeNotifier {
ViewState _state = ViewState.Idle;

ViewState get state => _state;

void setState(ViewState viewState) {
_state = viewState;
notifyListeners();
}
}

And Here is issue

> 
is not registered inside GetIt
I/flutter (19507): _RegistrationViewState#1f05d):
I/flutter (19507): Invalid argument (Object of type RegisterModel is not registered inside GetIt.
I/flutter (19507):  Did you forget to pass an instance name?
I/flutter (19507): (Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;)):
I/flutter (19507): RegisterModel
I/flutter (19507):
I/flutter (19507): When the exception was thrown, this was the stack:
I/flutter (19507): #0      GetIt.get 
package:get_it/get_it.dart:76
I/flutter (19507): #1      GetIt.call 
package:get_it/get_it.dart:87
I/flutter (19507): #2      new _BaseViewState 
package:mokoo_dog/…/views/base_view.dart:18
I/flutter (19507): #3      BaseView.createState 
package:mokoo_dog/…/views/base_view.dart:14
I/flutter (19507): #4      new StatefulElement 
package:flutter/…/widgets/framework.dart:3989
I/flutter (19507): #5      StatefulWidget.createElement 
package:flutter/…/widgets/framework.dart:802
I/flutter (19507): #6      Element.inflateWidget 
package:flutter/…/widgets/framework.dart:3082
I/flutter (19507): #7      Element.updateChild 
package:flutter/…/widgets/framework.dart:2887
I/flutter (19507): #8      ComponentElement.performRebuild 
package:flutter/…/widgets/framework.dart:3935
I/flutter (19507): #9      Element.rebuild 
package:flutter/…/widgets/framework.dart:3721
I/flutter (19507): #10     ComponentElement._firstBuild 
package:flutter/…/widgets/framework.dart:3907
I/flutter (19507): #11     StatefulElement._firstBuild 
package:flutter/…/widgets/framework.dart:4053
I/flutter (19507): #12     ComponentElement.mount 
package:flutter/…/widgets/framework.dart:3902
I/flutter (19507): #13     Element.inflateWidget 
package:flutter/…/widgets/framework.dart:3084
I/flutter (19507): #14     Element.updateChild 
package:flutter/…/widgets/framework.dart:2887
I/flutter (19507): #15     SingleChildRenderObjectElement.mount 
package:flutter/…/widgets/framework.dart:5092
I/flutter (19507): #16     Element.inflateWidget 
package:flutter/…/widgets/framework.dart:3084
I/flutter (19507): #17     Element.updateChild 
package:flutter/…/widgets/framework.dart:2887
I/flutter (19507): #18     ComponentElement.performRebuild 
package:flutter/…/widgets/framework.dart:3935
I/flutter (19507): #19     Element.rebuild 
package:flutter/…/widgets/framework.dart:3721
I/flutter (19507): #20     ComponentElement._firstBuild 
package:flutter/…/widgets/framework.dart:3907
I/flutter (19507): #21     ComponentElement.mount 
@MuhammamdArslanKhan
Copy link
Author

I think this error is misguiding us "Invalid argument (Object of type RegisterModel is not registered inside GetIt. " and the error should be that you are missing to register(I am not sure what we say when we include in the main function) locator in the main function
I followed the written tutorial and video as well there was one thing missing that we need to include setupLocator(); in the main function.
in the get_it package description, it is also missing.

void main() {
  setupLocator();   // I was missing setupLocator to regiter in then main function
  runApp(MyApp());
}

@escamoteur
Copy link
Collaborator

Error message will be improved in V4.0.0

@pradyuman98
Copy link

Error message will be improved in V4.0.0

Damn you saved my life.........Thanks man

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

3 participants