element data not refreshing - flutter

//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.

Related

Call a method on page pop

I'm pushing a new route like so in my app
Navigator.of(context)
.push<void>(
FilterTypesPage.routeFullScreen(context),
).then(
(value) {
log('PAGGGE POPPED');
},
),
static Route routeFullScreen(BuildContext context) {
return MaterialPageRoute<void>(
settings: const RouteSettings(name: routeName),
builder: (_) => BlocProvider.value(
value: BlocProvider.of<FeatureBloc>(context),
child: const FilterTypesPage(),
),
fullscreenDialog: true);
}
for some reason log('PAGGGE POPPED'); doesn't get called on page close
I'd like to trigger a bloc event or a function when I close this page
You should just call
Navigator.pop(context, someData);
from your RouteSettings where someData is the data you want to pass from the RouteSettings to the former page.
Then from your former page, you can perform your event handling inside the then block. The value inside the then block is the data that was passed from the RouteSettings page.
Alternatively, you can also use async-await instead of then in your former page.
onPressed: () async {
final someData = await Navigator.of(cotext).push(.....);
// Now perform your event handling which will be invoked after you pop `RouteSettings` page.
}

How to automatically navigate to a new page and send parameters after a fixed duration

I have a quiz page which has a button 'end quiz', which navigates to the new page when the user taps on it. It also sends info like questions attempted and options selected to the new page.
I want to have a timer which automatically ends the quiz and does all that the button did.
I have got an idea on how to navigate to a new page after a delay using this:
import 'dart:async';
Timer(Duration(seconds: 5), () {
// 5 seconds over, navigate to Page2.
Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2()));
});
link to where I got this info from
Now where I exactly do I place this so as to also get all the data from the widgets the user interacts with?
From your init Function Exit Use This :
#override
void initState() {
Timer(Duration(seconds: 5), () {
// 5 seconds over, navigate to Page2.
Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2(arguments: { send your data here ..... })));
});
)
Dont Forget to Use Stateful Widget
timer() async {
await Future.delayed(Duration(milliseconds: 5000));
//enter you code to save the data here here
Navigator.push(context, MaterialPageRoute(builder: (context) => child));
}

refresh previous page on Navigator.pop function

Is there any way to refresh the previous page/ stack when Navigator.pop(context) is called? I need to do the API calling of the previous page to refresh the state of the page. The navigator.pop will sometimes be an alert dialog or maybe a back button. Is there a way to do the API calling? ThankYou.
use the then function after you push another route.
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => MyHomePage(),
),
).then(
(value) {
if (value) {
//refresh here
}
},
);
and when you return to previous screen, provide the pop function a value that determines whether to perform some action.
Navigator.of(context).pop(true);
Previous page
void goToNextPage()async {
var refresh = await Navigator.push(_context, new MaterialPageRoute(
builder: (BuildContext context) => new nextPage(context))
);
if(refresh ) setState((){});
}
NextPage
Navigator.pop(context, true);

Flutter: Navigator.pop(context);

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()));
});

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