The problem I have is that I want to run a function before the buildcontext is rendered. What the function does is read the storage to get the token. The function works with Async Await. Why do I want to do this? Because in the build context I have a FutureBuilder and in this I need token data.
class ProfileScreen extends StatefulWidget {
ProfileScreen({Key? key}) : super(key: key);
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
dynamic token;
getToken()async{
token= await decodeJwt();
print("first this");
print(token);
return token;
}
@override
void initState() {
getToken();
super.initState();
}
@override
Widget build(BuildContext context) {
print("after this");
final profileService=Provider.of<ProfileService>(context);
final listDropDownService=Provider.of<ListDropDown>(context);
return FutureBuilder(
future: Future.wait([profileService.getSpecificWalker(token["id"]), listDropDownService.getDropDown("services")]),
builder: (context,dynamic snapshot){
as you can see i used some print also to see what the sequence was like. But first it prints "then this" and then "first this"
CodePudding user response:
What you said shows that you need state manager, because in one state you want to get token then when token get available, call the futureBuilder. I recommended bloC.
CodePudding user response:
My strongest suggestion would be to use some state management solution, such as flutter_bloc
, that is not directly dependent on the UI components.
If not, then why don't you just try and wrap the getToken()
method and the profileService.getSpecificWalker(token["id"])
call in a single async method and call that from the FutureBuilder.