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.