What is the best way to call nested future in Flutter ? Or flutter in general ?
In my app, I have to get the current user (which have its data in final storage = new FlutterSecureStorage();
.
From this user, I call an API which is by the user id. Theses two parts works fine independently (quite slow, but that's another story).
On many screen, I have to call asynchronious functions, I do this by using Future.delayed
, while my coworker is used to use something like this :
void initState() {
super.initState();
getCurrentUser();
}
void getCurrentUser() async {
user = await UserAuth.getCurrentUser();
}
this is my nested Futures. I need the current user to be loaded on my initState to load my publications. I have to get the user inside this because it can also be the not current user (this function will be used to retrieve current user but also other users publications).
class _UserPublicationsState extends State<UserPublications> {
List<Publication> listPublications;
List<Widget> list = [];
User user;
@override
void initState() {
super.initState();
Future.delayed(Duration.zero, () async {
await UserAuth.getCurrentUser().then((value) {
setState(() {
user = value;
UserWS.getPublicationsPerUsers(user).then((value) {
setState(() {
listPublications = value;
});
});
});
});
});
}
@override
Widget build(BuildContext context) {
if (listPublications == null) {
return FullPageLoading();
}
String title = "title";
return SafeArea(
child: Scaffold(
appBar: getSimpleAppBar(context, title),
body: SingleChildScrollView(
child: getSavedPublications(),
),
),
);
}
}