Reputation: 1355
I'm trying to display a snack bar when user logs out (I'm trying not to avoid using the fluttertoast package).
In my welcome screen, I have a button that goes to the login page:
child: MaterialButton(
child: Text('Login', style: TextStyle(color: Colors.white)),
onPressed: () async {
final logOutMessage = await Navigator.of(context).pushNamed(LoginScreen.routeName);
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(logOutMessage),
duration: Duration(seconds: 1),
));
}
In my login screen, I push and replace the routing stack to another page:
final authResult = await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
if (authResult.user != null) {
FirestoreInit.initDocs(authResult.user);
Navigator.of(context).pushReplacementNamed(Routing.routeName); // Routing is just another file name where I set up my PageView and Bottom Navigation Bar
}
In this routing file, a child widget is my home screen. My home screen has another widget which a custom drawer. In this customer drawer, I have the log out button:
ListTile(
onTap: () {
FirebaseAuth.instance.signOut();
// Fluttertoast.showToast(msg: 'You have been signed out');
Navigator.of(context).pop(ModalRoute.withName(Navigator.defaultRouteName));
},
leading: Icon(Icons.exit_to_app),
title: Text("Log Out"),
),
At first, I was getting the famous scaffold context error. I went to the link provided in the error message, and I used a builder. The snackbar appears now, but with a 'null' message.
I'm assuming that my message in the pop method is lost to another page. Should I just go back to using the flutter toast for this one?
Side note: I don't remember why I wrote the pop method that way. I changed it back from:
Navigator.of(context).pop(ModalRoute.withName(Navigator.defaultRouteName));
to:
Navigator.of(context).pop('You have logged out!');
I thought the routing stack should be different from the widget tree, but I feel it might be something to do with my widget tree, so I have included it.
On the login screen:
After the pushReplacement from the login screen (The pop method is in a listtile that's a sibling of the first Column child under CustomerDrawer at the bottom of the image):
Upvotes: 2
Views: 1318
Reputation: 1706
You can use Completer
to make your Welcome page await for the last route pushed not just the next route only.
In short, it will await your poped message even if you pushed 100 page after it.
final authResult = await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password);
if (authResult.user != null) {
FirestoreInit.initDocs(authResult.user);
final completer = Completer();
final result = await navigator.pushReplacement(
Routing.routeName,
result: completer.future,
);
completer.complete(result);
}
Upvotes: 3
Reputation: 1472
For future use, you can try this:
loginPage.dart
loginAction(BuildContext context) async {
final result = await Navigator.push(context, MterialPageRoute(
builder: (context) => secondPage()
));
Scaffold.of(context)..removeCurrentSnackBar();
if(result != null || result != "") {
loginScaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text(
result["msg"],
style: TextStyle(
color: Colors.white
),
),
duration: Duration(
seconds: result["duration"]
),
backgroundColor: result["color"],
)
);
}
}
secondPage.dart
logoutAction() {
String msg = "You have logged out!"
Navigator.pop(
context,
{
"color": Colors.green,
"msg": msg,
"duration": 2
}
);
}
Upvotes: 1