Git Product home page Git Product logo

Comments (8)

escamoteur avatar escamoteur commented on June 21, 2024 3

Where do you call your register function? If you do it directly in main before runApp it should not happen.

from get_it.

gersonfa avatar gersonfa commented on June 21, 2024 1

Sorry, I haven't had noticed you have answered, but yes, that solved my problem. Thanks!

from get_it.

escamoteur avatar escamoteur commented on June 21, 2024

Did this solve your problem?

from get_it.

MuthuHere avatar MuthuHere commented on June 21, 2024

sorry guys, i am trying to register with context in this case. If i do hot reload getting this error. I need context to register, so i did inside the
build(BuildCotext context){
setsetupLocator(context)
}

so how to do in this case. Thanks

from get_it.

josh-ksr avatar josh-ksr commented on June 21, 2024

@MuthuHere this is possible, but you'll need some logic to handle it inside your GetIt setup code. Here's an example:

void setUpServiceLocator(BuildContext context) {
  if (getIt.isRegistered<AppLocalizations>()) {//setup was called before, we only re-register what depends on context
    getIt.unregister<AppLocalizations>();
    getIt.registerSingleton<AppLocalizations>(AppLocalizations.of(context));
    return;
  }
getIt.registerSingleton<AppLocalizations>(AppLocalizations.of(context));
//do all your other setup that's independent of context here
}

from get_it.

escamoteur avatar escamoteur commented on June 21, 2024

@MuthuHere don't do this inside a build function. It can be called anytime. Use a StatefulWidget and access the context in didChangeDependency.

What do you want to register that needs a context? Maybe there is a better way to do this.

@josh-ksr don't use the .of to reach something to register in get_it. Instead register Applocalization before you call runApp directly in get_it as it doesn't depend on the context.

from get_it.

josh-ksr avatar josh-ksr commented on June 21, 2024

@escamoteur I don't think it is possible to use the new flutter localization system without context, at least i haven't found a way to use it without context yet. But sometimes there's the need to access it independent of a view, for example when creating local notifications inside the business logic layer.
That's why i added a builder to the MaterialApp and setup GetIt inside it, seems to work without problems so far even with hot reload. Or is there any problem with this approach that i just haven't seen yet? Here's some example code:

  Widget build(BuildContext context) {
    return MaterialApp(
          builder: (context, widget) {
            setUpServiceLocator(context);
            return FutureBuilder(
                future: getIt.allReady(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (snapshot.hasData) {
                    return widget;
                  } else {
                    return Container(color: Colors.white);
                  }
                });
          },
        );
}

from get_it.

escamoteur avatar escamoteur commented on June 21, 2024

yes, that should work fine. I was just looking at the examples here and didn't see why you need the context when creating the AppLocalisation instance.

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.