class _WorterBuchState extends State<WorterBuch> with TickerProviderStateMixin {
var wordPair= '' ;
@override
void initState(){
super.initState();
load();
}
Future<void> load() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('key', 'value');
wordPair = await "key : " + (prefs.getString('key') as String);
}
@override
Widget build(BuildContext context) {
print(wordPair);
I have this class and this async function. I run the async function in the initState call but when build method runs it prints nothing. How can i solve this problem or how can i wait how the Future to finish? Can anybody please helpp
2
Answers
Future method takes some frame to fetch data, try using FutureBuilder like
More about FutureBuilder
Running the
build
method cannot be delayed and this method cannot be asynchronous because that would block/delay the rendering of the UI making the app unresponsive.What you need to do is store the
Future
in the state and useFutureBuilder
: