Stateless Widget
CustomButton.build(
label: 'login',
onPressed: () {
presenter.login(context,username,password);
},
);
Presenter class (where we have all the logic)
class Presenter {
Future<void> login(BuildContext context,String username, String password) async {
showDialog(context);
var result = await authenticate(username,password);
int type = await getUserType(result);
Navigator.pop(context); // to hide progress dialog
if(type == 1){
Navigator.pushReplacementNamed(context, 'a');
}else if(type == 2){
Navigator.pushReplacementNamed(context, 'b');
}
}
Future<int> getUserType(User user) async {
//.. some await function
return type;
}
}
Now we are getting Do not use BuildContexts across async gaps. lint error on our presenter wile hiding the dialog and screen navigation.
What is the best way to fix this lint.
Don't stock
contextdirectly into custom classes, and don't use context after async if you're not sure your widget is mounted yet.I see the better practice to fix it is to set an
onSuccessmethod as parameter which will have theNavigator.pop(context);from your UI, but call it inside your main method: