Flutter: Navigator.pop(context); - flutter

I pass a page with this code below.
Navigator.push(context,
MaterialPageRoute(builder: (context) => FileFolder()));
for example, I change some data in FileFolder(); page. And I want to get this data to my first page what I change data. I use Navigator.pop(context); but this code is not run initstate((){});. How can I refresh my first page?

//first page
Future data = await Navigator.push(context,
MaterialPageRoute(builder: (context) => FileFolder()));
setState(() {
myData = data;
});
//second page FileFolder
Map data = {};
Navigator.pop(context,data);

You can write your code inside initstate((){}); method like below:
Future.delayed(Duration.zero, () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => FileFolder()));
});

Related

how to send object when i pop? (flutter)

Context: I'm creating an app for sales and when I'm on the sales screen I have a button (Add item), this button takes me to the items grid.
my difficulty is turning on the sales screen with the selected item.
onTap: () => isSelecting ? Navigator.of(context).pop() : null,
You can use this
Navigator.pop(context, yourObject);
onPressed: () async {
newProduto = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => ProdutosPage(selecting: true)));
setState(() {
produtos.add(newProduto);
});
}

How can I access the fields in firebase firestore?

I am working on ride sharing app. I just want that whenever I close the application after the login then next time I will redirect to the home screen.
I have two options login as driver or pessenger ,but whenever I loggedin as pessenger and closed the application, it will redirect me to the Driver home screen. I just want to access status field so that I can make a conditional difference between driver and pessenger
I am pasting the code below, kindly check it and help me if you can!!
class SplashServices {
void isLogin(BuildContext context) {
final auth = FirebaseAuth.instance;
final user = auth.currentUser;
if (user != null) {
Timer(
const Duration(seconds: 3),
() => Navigator.push(context,
MaterialPageRoute(builder: (context) => const DriverPost())));
} else {
Timer(
const Duration(seconds: 3),
() => Navigator.push(context,
MaterialPageRoute(builder: (context) => const GettingStarted())));
}
}
}
I'm not sure I entirely understand the question, but with the way that your doc is set up with the status field, which I'm assuming will have two options, driver or passenger, you could access the value through a function like this or similar to it:
Future<void> checkUserStatus() {
final driver = FirebaseAuth.instance.currentUser;
final driverDoc = await FirebaseFirestore.instance.collection('driver').doc(driver!.uid).get();
if (userDoc.data()!['status'] == 'driver'){
Navigator.push(context,
MaterialPageRoute(builder: (context) => const DriverPost()))
} else{
Navigator.push(context,
MaterialPageRoute(builder: (context) => const GettingStarted()))
}
}
Let me know if that helps.

element data not refreshing

//calling a different class for dialog on submit click it's not updating list data
It's refreshing data but not going to the last opened tab on the second tab I perform this action but on tap, it's going on the first tab.
onTap: () {
setState(() {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) =>
EmployeePersonalDetailsUpdateActivity(_token),));
widget.listener.onEmployeeFamilyClick(_day, _month, _year,
_working);
});
Navigator.pop(context,true);
}
Your are calling your widget.listener after you navigate, call it before you navigate the route & if you want to pass the api response to the EmployeePersonalDetailsUpdateActivity widget, you must handle it, and it's your choice when you want to refresh your screen.
setState(() {
widget.listener.onEmployeeFamilyClick(_day,_month,_year,_working);
Navigator.pushReplacement(context, MaterialPageRoute(builder:
(context) => EmployeePersonalDetailsUpdateActivity(_token),));
});
There's another way:
widget.listener.onEmployeeFamilyClick(_day,_month,_year,_working).then((value) {
Navigator.pushReplacement(context, MaterialPageRoute(builder:
(context) => EmployeePersonalDetailsUpdateActivity(_token), value));
});
you can navigate your widget and pass the value of the response and show the updated value on the widget.

How I can block user to go back to the previous page in flutter

I uses to navigate to a new screen :
Navigator.push(context, MaterialPageRoute(builder: (context) => CardsListView(pageId)))
the problem is the user can go back to the previous page and I don't want to allow him to be able to do that. Anyone know how I can do that?
You can remove the route.
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => ResultsPage()),
(route) => route.isFirst);
Or
void _logout() {
Navigator.pushNamedAndRemoveUntil(context, "/newRouteName", (r) => false);
}
There are other methods which may be useful:
https://docs.flutter.io/flutter/widgets/Navigator/replace.html
https://docs.flutter.io/flutter/widgets/Navigator/pushReplacement.html
https://docs.flutter.io/flutter/widgets/Navigator/popAndPushNamed.html

Undefined name 'context' in flutter navigation

I want to navigate into one page to another an I used following code to that
Create route
final routes = <String,WidgetBuilder> {
DashboardIesl.tag : (context)=>DashboardIesl()
};
navigation to button click
onPressed: () {
Navigator.push(context, new MaterialPageRoute(
builder: (context) => new DashboardIesl()
));
},
It gives the error message as follows
Undefined name 'context'.
please show me more code,context is only available in statefull widgets,
onPressed: () { Navigator.push(
context,
MaterialPageRoute(builder: (context) => DashboardIesl()), );}
I think this will help you
You have to write onPressed() with in the class.