Skip to content

How to use Async? #71

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
RakaAlrian opened this issue Apr 30, 2020 · 1 comment
Closed

How to use Async? #71

RakaAlrian opened this issue Apr 30, 2020 · 1 comment

Comments

@RakaAlrian
Copy link

RakaAlrian commented Apr 30, 2020

Hello, I have future function to load color from json file like this

Future<ColorKey> load() async {
    final path = "assets/colors/light.json";
    final strJson = await rootBundle.loadString(path);
    Map<String, dynamic> data = jsonDecode(strJson);

    return ColorKey(Map<String, String>.from(data));
  }

I tried to register that function using get_it like this
serviceLocator.registerFactoryAsync<ColorKey>(() => ColorBase().load());
and this
serviceLocator.registerLazySingletonAsync<ColorKey>(() => ColorBase().load());
but both shows error.

Then I tried this

final color = await ColorBase().load();
serviceLocator.registerFactory<ColorKey>(() => color);

and this

final color = await ColorBase().load();
serviceLocator.registerLazySingleton<ColorKey>(() => color);

both are working, but then when I pass ContextUtils as parameter to load() function like this

serviceLocator.registerFactoryParam<ContextUtils, BuildContext, void>((context, _) => ContextUtilsImpl(context));

final color = await ColorBase().load(serviceLocator());
serviceLocator.registerLazySingleton<ColorKey>(() => color);

it's not working, and I got this error 'context != null': is not true.
Any suggestion?

@escamoteur
Copy link
Collaborator

Hi,
two problems in your code

  1. add an async to your registration

`serviceLocator.registerFactoryAsync(() async => ColorBase().load());

  1. Where do you access ContextUtils when you get this error?

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