GithubHelp home page GithubHelp logo

Comments (10)

escamoteur avatar escamoteur commented on August 26, 2024

sure, that is expected. getAsync returns a Future so you have to write it this way:

(await GetIt.I.getAsync<IAppModel>()).addListener(update);

which is what you do when you use the then.

from get_it.

warcayac avatar warcayac commented on August 26, 2024

I've rewritten initState method to this:

  @override
  void initState() async {
    (await GetIt.I.getAsync<IAppModel>()).addListener(update);

    super.initState();
  }

Now I get this error message:

Connecting to VM Service at ws://127.0.0.1:40499/I25uskXCZ5E=/ws
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Builder:
_HomePageState.initState() returned a Future.
State.initState() must be a void method without an `async` keyword.
Rather than awaiting on asynchronous work directly inside of initState, call a separate method to do
this work without awaiting it.

So I rewrote it again:

  @override
  void initState() {
    _adder();
    super.initState();
  }

  void _adder() async => (await GetIt.I.getAsync<IAppModel>()).addListener(update);

Now I get this error message:

Connecting to VM Service at ws://127.0.0.1:33589/jvi4nx9cHWI=/ws
Rebuilt screen
Error: Bad state: You can only access registered factories/objects this way if they are created asynchronously
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3  throw_
...

from get_it.

escamoteur avatar escamoteur commented on August 26, 2024

from get_it.

warcayac avatar warcayac commented on August 26, 2024

app_model.dart

import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart';

abstract interface class IAppModel extends ChangeNotifier {
  void incrementcounter();
  int get counter;
}

class AppModel extends IAppModel {
  var _counter = 0;

  AppModel() {
    /// lets pretend we have to do some async initialization
    Future
      .delayed(const Duration(seconds: 3))
      .then((_) => GetIt.I.signalReady(this))
    ;
  }

  @override
  int get counter => _counter;

  @override
  void incrementcounter() {
    _counter++;
    notifyListeners();
  }
}

di.dart

import 'package:get_it/get_it.dart';

import '../model/app_model.dart';

void configureDependencies() {
  GetIt.I.registerSingleton<IAppModel>(AppModel(), signalsReady: true);
}

main.dart

void main() async {
  configureDependencies();
  runApp(const MyApp());
}

from get_it.

escamoteur avatar escamoteur commented on August 26, 2024

from get_it.

warcayac avatar warcayac commented on August 26, 2024

I'm sorry but I'm a little confused with your answer. In your example code you wrote:

    // Alternative:
    // getIt.getAsync<AppModel>().addListener(update);

This would indicate that this code

    GetIt.I
      .isReady<IAppModel>()
      .then((_) => GetIt.I<IAppModel>().addListener(update))
    ;

can be replaced by this single line: getIt.getAsync<AppModel>().addListener(update); however you suggest an additional change and it brings other changes and errors. After you say:

If you doesn't have any asynchronous inititialisation function, you don't need to use already or getAsync at all

So this alternative way you wrote in your example code doesn't apply to it, it's correct? If so, shouldn't there be another example where getAsync is corectly used and not here that brings confusion? If I'm wrong, I ask you please to show me the correct way to use it in your code.

from get_it.

escamoteur avatar escamoteur commented on August 26, 2024

from get_it.

warcayac avatar warcayac commented on August 26, 2024

https://github.com/fluttercommunity/get_it/blob/master/example/lib/main.dart

line 50

from get_it.

escamoteur avatar escamoteur commented on August 26, 2024

from get_it.

escamoteur avatar escamoteur commented on August 26, 2024

I added a comment in the example for clarification

from get_it.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.