How to fix the problem of 'Object of type XXXXX is not registered inside GetIt' in flutter?

15,463

Solution 1

don't forget to call setupLocator in your main app file before App init

  setupLocator();
  runApp(MyApp());

Solution 2

Try the following code on your main function.

GetIt locator = GetIt.instance;

void main() {

  locator.registerLazySingleton<Api>(() => new Api('teams')) ;

  runApp(MyApp());
}

Or try to have a separate file for your service locator like:

service_locator.dart

GetIt sl = GetIt.instance;

final httpLink = HttpLink(...);

final GraphQLClient client = GraphQLClient(
  cache: InMemoryCache(),
  link: httpLink,
);

void setUpServiceLocator() {
  // Services
  sl.registerSingleton<LocationsService>(LocationsService(http.Client()));
  ...

  // Managers
  sl.registerSingleton<LocationsManager>(LocationsManager());
  ...
}

main.dart

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  setUpServiceLocator();
  runApp(MyApp());
}

some_file.dart

import 'package:my_awesome_app/service_locator.dart';

...
// call it anywhere you want on your code
sl<LocationsManager>().updateLocationsCommand.execute(query);

// or
sl.get<LocationsManager>().updateLocationsCommand.execute(query);

Solution 3

Try version GetIt. I use ^3.0.3. It work for me. Maybe you use old version.

Solution 4

answer for those who face this error in 2021 with dependency get_it: ^5.0.3

make class locator. dart

import 'package:get_it/get_it.dart';
import 'package:rest_api_work/Service/note_service.dart';

final locator = GetIt.instance;

void setUpLocator() 
{

locator.registerLazySingleton<NoteService>(() =>NoteService());

}

Now call this method setUpLocator in main.dart

  void main() 
 {

   setUpLocator() ;
   runApp(MyApp());
  }

and now make a property of locator class where you show your data because my data is list type you can see in noteservice.dart that's why I receive this property in the List type class

List<NoteForListing> notes = [];
 void initState()  {
         List<NoteForListing> service =  locator.get<NoteService>().getNotesList();
 
    setState(() {
      notes = service;    });
  
       super.initState();   }

and at last noteserverice.dart

import 'package:rest_api_work/models/note_for_listing.dart';

class NoteService
{
  List<NoteForListing> getNotesList()
  {
     return
     [
    new NoteForListing(
      noteID :"1",
      noteTitle:"Note 1",
      createDateTime:DateTime.now(),
      latestEditDateTime:DateTime.now()
    ),
       new NoteForListing(
      noteID :"2",
      noteTitle:"Note 2",
      createDateTime:DateTime.now(),
      latestEditDateTime:DateTime.now()
    ),
       new NoteForListing(
      noteID :"3",
      noteTitle:"Note 3",
      createDateTime:DateTime.now(),
      latestEditDateTime:DateTime.now()
    )
  ];
  
  }
}
Share:
15,463
Arpit Bansal
Author by

Arpit Bansal

I am your normal geek who spends most of his time staring at the computer screen and trying my hand at more and more things.

Updated on June 14, 2022

Comments

  • Arpit Bansal
    Arpit Bansal almost 2 years

    I am trying to use get_it in order to create a singleton object to be used. I do not wish to use multiple objects of API which is connecting to Firebase. The singleton object is that of Api call for firebase.

    I have used the following code

    locator.registerLazySingleton<Api>(() => new Api('teams')) ;
    

    while following code works

    locator.registerLazySingleton<TeamViewModel>(() => new TeamViewModel()) ;
    

    The structure of Api class is as follows:

    class Api{
      final Firestore _db = Firestore.instance;
      final String path;
      CollectionReference ref;
      
      Api( this.path ) {
        ref = _db.collection(path);
      }
    
      Future<QuerySnapshot> getDataCollection() {
         return ref.getDocuments() ;
      }
    }`
    

    This is how I use the API singleton object:

    Api _api = locator<Api>();
    

    while the following code works fine:

    Api _api = Api('team');
    

    I get the following error in console:

    I/flutter ( 2313): The following _Exception was thrown building MultiProvider:

    I/flutter ( 2313): Exception: Object of type Api is not registered inside GetIt

    I wish to know if this is even possible of using getit is not the right way to go about this.

  • Arpit Bansal
    Arpit Bansal over 4 years
    Still the issue remains the same.
  • Simas Joneliunas
    Simas Joneliunas over 4 years
    Your code does exactly the same thing as one of the examples in the earlier answer
  • devmike01
    devmike01 over 2 years
    There is no difference between this code snippet and that of the original post.
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.