Skip to content

Singleton not being instantiated #127

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
andreddie81 opened this issue Nov 6, 2020 · 5 comments
Closed

Singleton not being instantiated #127

andreddie81 opened this issue Nov 6, 2020 · 5 comments

Comments

@andreddie81
Copy link

andreddie81 commented Nov 6, 2020

I've got a very simple flutter app, where, at the very beginning I have:

---- main.dart ----

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

--- dependencies.dart

import 'package:get_it/get_it.dart';
import 'package:sample_app/network/main_api.dart';

GetIt getIt = GetIt.instance;

void initializeDependenciesInjection() {
  getIt.registerSingleton<MainApi>(MainApi(), signalsReady: true); 
}

----- MyApp.dart ------

MainApi api;

@override
  void initState() {

    print("INIT IS CALLED");

    getIt.isReady<MainApi>().then((_) {
      print("API HAS BEEN INITIALIZED");
      api = getIt<MainApi>();
    });
    super.initState();
  }

And when I try to use api on any other part of the class I get a Null exception, even inside a FutureBuildwe where I am using as future: getIt.allReady(),

The message API HAS BEEN INITIALIZED is never printed.

what am I doing wrong here?? tried to follow the example as much as I can.

@escamoteur
Copy link
Collaborator

Hi,

I'm not fully sure. Some clarificaions:

1.: using isReady, allReady only makes sense if you have some async initialization in the objects that you register in get_it.
2. if you set signalReady=true getIt expects you to call signalReady when your object is ready.

if you could show me a bit more code of your MainMapi constructor it might help me to help you further.

@andreddie81
Copy link
Author

Hi thanks for you fast response,

I do not really have any async initializations in my object, maybe there I am doing something wrong.

Well the MainApi is very very basic at the moment, not sure if that will help but, here it is:

import 'package:http/http.dart' as http;

class MainApi {

  final _hostAddress = "xxxxxxxxxxxxxxx";

  Future<http.Response> getAllUsers() async {
    return await http.get(
      '$_hostAddress/getAllUsers', headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8'
      },
    );
  }

}

@escamoteur
Copy link
Collaborator

In that case you don't have to use isReady or allReady at all because Dart is singel threaded.
Just don't pass signalReady you can directly access MainApi after the registration.
If your object needs some async work before you can use it like reading from shared_preferences you can come back to this here.

@andreddie81
Copy link
Author

Thank you so much, I will try it!

@andreddie81
Copy link
Author

It works, you were right, no need for signalReady at all. Thanks again!

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

2 participants