I'm a looking for a way to load async data on InitState method, I need some data before build method runs. I'm using a GoogleAuth code, and I need to execute build method 'till a Stream runs.
My initState method is:
@override void initState () { super.initState(); _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) { setState(() { _currentUser = account; }); }); _googleSignIn.signInSilently(); }I will appreciate any feedback.
417 Answers
You can create an async method and call it inside your initState
@override
void initState () { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_){ _asyncMethod(); });
}
_asyncMethod() async { _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) { setState(() { _currentUser = account; }); }); _googleSignIn.signInSilently();
} 10 As of now using .then notation seems to work:
// ... @override initState() { super.initState(); myAsyncFunction // as suggested in the comment // .whenComplete() { // or .then((result) { print("result: $result"); setState(() {}); }); } //... 8 Method 1 : You can use StreamBuilder to do this. This will run the builder method whenever the data in stream changes.
Below is a code snippet from one of my sample projects:
StreamBuilder<List<Content>> _getContentsList(BuildContext context) { final BlocProvider blocProvider = BlocProvider.of(context); int page = 1; return StreamBuilder<List<Content>>( stream: blocProvider.contentBloc.contents, initialData: [], builder: (context, snapshot) { if (snapshot.data.isNotEmpty) { return ListView.builder(itemBuilder: (context, index) { if (index < snapshot.data.length) { return ContentBox(content: snapshot.data.elementAt(index)); } else if (index / 5 == page) { page++; blocProvider.contentBloc.index.add(index); } }); } else { return Center( child: CircularProgressIndicator(), ); } }); }In the above code StreamBuilder listens for any change in contents, initially its an empty array and shows the CircularProgressIndicator. Once I make API call the data fetched is added to contents array, which will run the builder method.
When the user scrolls down, more content is fetched and added to contents array which will again run builder method.
In your case only initial loading will be required. But this provides you an option to display something else on the screen till the data is fetched.
Hope this is helpful.
EDIT:
In your case I am guessing it will look something like shown below:
StreamBuilder<List<Content>>( stream: account, // stream data to listen for change builder: (context, snapshot) { if(account != null) { return _googleSignIn.signInSilently(); } else { // show loader or animation } });Method 2: Another method would be to create an async method and call it from you initState() method like shown below:
@override void initState() { super.initState(); asyncMethod(); } void asyncMethod() async { await asyncCall1(); await asyncCall2(); // .... } 4 Create anonymous function inside initState like this:
@override
void initState() { super.initState(); // Create anonymous function: () async { await _performYourTask(); setState(() { // Update your UI with the desired changes. }); } ();
} 4 @override void initState() { super.initState(); asyncInitState(); // async is not allowed on initState() directly } void asyncInitState() async { await yourAsyncCalls(); } 5 Previous Answer!!
You can set a Boolean value like loaded and set it to true in your listen function and make your build function return your data when loaded is set to true otherwise just throw a CircularProgressIndicator
Edited -- I would not suggest calling setState in a method you call in initState. If the widget is not mounted while the setState is called (as the async operation completes) an error will be reported. I suggest you use a package after_layout
Take a look at this answer for better understanding setState in initState :
This post will give you an idea to know when the app finishes the build method. So that you can wait for your async method to setState after widget is mounted :
You can create an async method and call it inside your initState
@override void initState() { super.initState(); asyncMethod(); ///initiate your method here }
Future<void> asyncMethod async{ await ///write your method body here
} Per documentation at
initState() { super.initState(); Future.microtask(() => context.read<MyNotifier>(context).fetchSomething(someValue); );
} initState() and build cannot be async; but in these, you can call a function that is async without waiting for that function.
How about this?
@override
void initState() { //you are not allowed to add async modifier to initState Future.delayed(Duration.zero,() async { //your async 'await' codes goes here }); super.initState();
} Sample code:
@override void initState() { super.initState(); asyncOperation().then((val) { setState(() {}); print("success"); }).catchError((error, stackTrace) { print("outer: $error"); });
//or asyncOperation().whenComplete(() { setState(() {}); print("success"); }).catchError((error, stackTrace) { print("outer: $error"); }); } Future<void> asyncOperation() async { await ... ; } 0 As loading or waiting for initial state is a (generally) aone off event FutureBuilder would seem to be a good option as it blocks once on an async method; where the async method could be the loading of json config, login etc. There is an post on it [here] in stack.(Flutter StreamBuilder vs FutureBuilder)
@override void initState() { super.initState(); _userStorage.getCurrentUser().then((user) { setState(() { if (user.isAuthenticated) { Timer.run(() { redirectTo(); }); } }); }); } void redirectTo() { Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => new ShopOrders())); } I would strongly suggest using a FutureBuilder. It takes care of executing the async function and building the widget according to the result!Here's a link to a short intro video and the documentation.
Code Example:
Future<void> initControllers() async { for (var filePath in widget.videoFilePaths) { var val = VideoPlayerController.file(File(filePath)); await val.initialize(); controllers.add(val); } } @override Widget build(BuildContext context) { FutureBuilder( future: initControllers(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return YourWidget(); } else { return const ProgressIndicator(); } }, ));} Tried all suggestions, none would keep my build from starting after the async method that I need in initState() finish, except one: the trick of having a a bool variable in the State class (let's call it _isDataLoaded) that is initialized to false upon definition, set to true inside a setState() that is invoked when the async function finishes inside initState(). In the build, condition a CircleProcessIndicator() or your Widget depending on the value of this variable.
I know it's dirty because it could break the build, but honestly nothing else that would make more sense -such as running super.initState() upon completion of the async function- has worked for me.
I came here because I needed to fetch some files from FTP on program start. My project is a flutter desktop application. The main thread download the last file added to the FTP server, decrypts it and displays the encrypted content, this method is called from initState(). I wanted to have all the other files downloaded in background after the GUI shows up.
None of the above mentioned methods worked. Constructing an Isolate is relatively complex.
The easy way was to use the "compute" method:
- move the method downloading all files from the FTP out of the class.
- make it an int function with an int parameter (I do not use the int parameter or the result)
- call it from the initState() method
In that way, the GUI shows and the program downloads the files in background.
void initState() { super.initState(); _retrieveFileList(); // this gets the first file and displays it compute(_backgroundDownloader, 0); // this gets all the other files so that they are available in the local directory }
int _backgroundDownloader(int value) { var i = 0; new Directory('data').createSync(); FTPClient ftpClient = FTPClient(' user: 'maxmusterman', pass: 'maxmusterpasswort'); try { ftpClient.connect(); var directoryContent = ftpClient.listDirectoryContent(); // .. here, fileNames list is reconstructed from the directoryContent for (i = 0; i < fileNames.length; i++) { var dirName = ""; if (Platform.isLinux) dirName = 'data/'; else dirName = r'data\'; var filePath = dirName + fileNames[i]; var myDataFile = new File(filePath); if (!myDataFile.existsSync()) ftpClient.downloadFile(fileNames[i], File(filePath)); } } catch (err) { throw (err); } finally { ftpClient.disconnect(); } return i; 0 I have used timer in initState
Timer timer;
@override
void initState() { super.initState(); timer = new Timer.periodic(new Duration(seconds: 1), (Timer timer) async { await this.getUserVerificationInfo(); });
}
@override
void dispose() { super.dispose(); timer.cancel();
}
getUserVerificationInfo() async { await someAsyncFunc(); timer.cancle();
}