1

I have some code I need to run in my initState() method as it initialises a late variable widgets in the build method depend on. The problem is the method I want to run is asynchronous as I am loading data from the disk using the Shared Preferences package. I tried making the method asynchronous but I get an error in my IDE saying that the initState() method can't be asynchronous. I tried looking up solutions and they said that you can run them without awaiting in the initState() method. I tried this but the problem is that it doesn't await the initialisation so I get the red error screen saying the variable hasn't been initialised. I originally was going to run this in a constructor of a custom class I had that deals with the data but I also get an error saying that the constructor method can' t be async.

My IDE suggested using the didChangeDependencies() as it can be async, however, the build method doesn't await it.

I tried all the answers in this question (Is there a way to load async data on InitState method?) that didn't suggest a StreamBuilder as I am not trying to load a stream that has to do with Authentication. I also tried to change around the order calling super.initState(); before and after the code, etc. and it didn't work.

  • The link contains an example of how to use a FutureBuilder. There is no way to have an async initState (or other init) method, you have to handle the async in your widget. – nvoigt May 02 '21 at 09:58
  • @nvoigt Is there a way to run some initialisation code other than the initState method? I have a variable that needs to be loaded from the disk that the widget depends on. I don't need to use the initState specifically just need to make the build method wait for a variable to load. Thanks for the help. –  May 02 '21 at 10:03
  • @nvoigt Using a FutureBuilder worked, I still get an exception but no red screens in the app. Is the exception a problem or do you know how I can fix it? The exception I get is that the late variable has not yet been initialised. –  May 02 '21 at 10:32
  • Well, you could just not make it "late", that should get rid of the exception. – nvoigt May 02 '21 at 10:36
  • Alright, thanks for the help! –  May 02 '21 at 10:38

1 Answers1

1

make a function that is async and perform your asynchronous work in it .. then call it in initState without async await keywords but like this

 @override
  void initState() {
    checkLoggedIn().then((user) {
    
     // do anything with your data here
   
    });
    super.initState();
  }
Moaid ALRazhy
  • 1,604
  • 1
  • 3
  • 13
  • I still run into the same problem. I am not trying to do anything with the data once it loads, I just want to load it before the build method. I have a provider at the root of my app with a custom class. The class contains a few variables like an input decoration, etc. as well as a variable called themeMode which stores the current theme mode the user selected. The user can select either system, light, or dark. I want to load this from the disk and I have a method to do that in the class. I have the material App theme mode to the `themeMode` variable in the class and I want to load it. –  May 02 '21 at 10:19
  • well what will be displayed in screen while loading from desk ? – Moaid ALRazhy May 02 '21 at 10:32
  • Umm not sure, a black screen or loading screen perhaps? Guess I can initialise the code there. Thanks for the help! –  May 02 '21 at 10:37