I do log out and then log in with another user but I find that the profile page and other pages are still on the same view before logging out to the previous user
I used
Get.offAllNamed('/pageName');
and
Get.deletAll();
and
Get.delete<controller>();
and
Get.offUntil(GetPageRoute(page: () => DirectionScreen()), ModalRoute.withName('/direct') );
Timer(Duration(milliseconds: 300), ()=>Get.delete<PageNavigationController>());
and
Navigator.of(context)
.pushAndRemoveUntil(
CupertinoPageRoute(
builder: (context) => DirectionScreen()
),
(_) => false,
);
and flutter_phoenix package
Phoenix.rebirth(Get.context!);
and path_provider package.
nothing do the work
I use restart_app package and its do the work
Related
Whenever I run the application at that time homescreen is only shows for a maximum of 5 seconds and without a click, I navigate to another page.
Which second page is product detail page, and on first page, product was displaied by catedories.
itemBuilder: (context, index) => ItemCard(
product: products[index],
press: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsScreen(
product: products[index],
),
)),
)),
),
),
],
);
}
}```
Error :
The following assertion was thrown while handling a gesture:
Assertion failed: file:///C:/src/flutter/flutter/packages/flutter/lib/src/widgets/navigator.dart:4842:12
!_debugLocked
is not true
You have to make sure the widget tree has built before navigating you can do that by warpping your function like this
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.push(context, MaterialPageRoute(builder: (_) =>
MyPage()));
});
I have 2 pages: Page1() and Page2() and a Drawer menu.
To go Page2() I use (inside Drawer):
ListTile(
title: Text("Go to Page 2"),
onTap: () {
Navigator.pop(context); // close Drawer
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => Page2(),
));
},
),
And when user is in Page2() and pressed back button (android devices). I want to come back Page1() and refresh it. I dont know how to do that.
I found this:
Navigator.of(context).push(new MaterialPageRoute(
builder: (_) => new Page2()))
.then((val) => _refreshDataInPage1());
But only works when I use in a function inside Page1(). I can't understand how implement that in Drawer.
Any suggestions?
Use Navigator.pop(context)
Navigator Class Doc
I'm having a little error I can't seem to figure out, and I believe it's due to my Alert Dialog implementation. I'm using the flutter Provider package, and opening my Dialog like this:
_openSearchHistory(
BuildContext context, TextEditingController searchController) {
final searchModel = Provider.of<SearchModel>(context, listen: false);
showDialog(
context: context,
builder: (_) => ChangeNotifierProvider<SearchModel>.value(
value: searchModel,
child: DialogSearchHistory(
searchHistory: searchModel.searchHistory,
searchController: searchController,
),
));
}
The problem is when on android and the user presses the back button, the Dialog does not close, but the page behind the Dialog pops back a page. I have a close button on the Dialog that successfully closes the dialog, but I want users to be able to use the back button for a better experience. The Dialog does close if the user clicks outside of it as well. I've tried wrapping the Dialog with WillPopScope, but it does not get called on a back button press.
Could anyone shed some light on what I'm doing wrong here?
I have tried wrapping the Widget in the calling method with WillPopScope
showDialog(
context: context,
builder: (_) => ChangeNotifierProvider<SearchModel>.value(
value: searchModel,
child: WillPopScope(
onWillPop: () {
Navigator.of(context).pop();
return Future.value(false);
},
child: DialogSearchHistory(
searchHistory: searchModel.searchHistory,
searchController: searchController,
),
),
));
As well as wrapping the Alert Dialog itself in the build function
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
Navigator.of(context).pop();
return Future.value(false);
},
child: AlertDialog(
backgroundColor: kCardColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),...
Neither have worked. The page behind the Dialog pops back, but the Dialog stays put. onWillPop is never hit by the debugger
Late to the party but setting useRootNavigator to false inside showDialog worked for me.
showDialog(
context: context,
useRootNavigator: false,
builder: (context) {
return AlertDialog();
},
);
If the application has multiple Navigator objects, it may be necessary to call Navigator.of(context, rootNavigator: true).pop(result) to close the dialog rather than just Navigator.pop(context, result).
I hope this will close the alert dialog If you have multiple navigator objects. Give it a try
onWillPop: (){
Navigator.of(context, rootNavigator: true).pop();
return Future.value(false);
},
OR
Try Navigator.pop(context);, this will call internally Navigator.of(context).pop() method.
Official Implementation of Navigator.pop() method.
static void pop<T extends Object>(BuildContext context, [ T result ]) {
Navigator.of(context).pop<T>(result);
}
Doc - https://api.flutter.dev/flutter/widgets/Navigator/pop.html
Hi there i face similar issue and the below method worked for me as Vinoth vino tell wrap your page in WillPopScope widget and try to add pop function there like
body: WillPopScope(
onWillPop: (){
Navigator.of(context).pop();
return Future.value(false);
},
child:...your code
I want to pass values from 2nd page to 1st page with navigator.pop and refresh or reload my 1st page with new values in initstate or any other work around?
I am able to get these values in 1st page but not able to refresh or reload the page with new values in initstate where i want to pass new data in API n get response...
any help?
I am redirecting to list page from this...
getBrand(BuildContext context) async {
tempBrand = await Navigator.push(context,
MaterialPageRoute(builder: (context) => BrandList(widget.subCatID)));
selectedBrand = tempBrand.name;
selectedBrandID = tempBrand.id;
}
now here i am passing those values in navigator.pop
Getting value here and passing back to 1st page.
Container(
padding: EdgeInsets.all(10),
child: ListView.separated(
separatorBuilder: (context, index) =>
Divider(color: Color(0xffcccccc)),
itemCount: prodBrands.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
child: InkWell(
onTap: () {
tempProdBrand = ProdBrandListModel.Message(
id: prodBrands[index].id,
name: prodBrands[index].name,
);
Navigator.pop(context, tempProdBrand);
},
child: Container(
child: Text(prodBrands[index].name),
padding: EdgeInsets.all(10.0),
),
),
);
}),
)
Want to use new values here...
submitProduct() {
api
.newbiz(
selectedBrandID,
)
.then((response) {
setState(() {
productID = response.message;
});
});
setState(() {});
}
Simply want to refresh my page from initstate or any other way when i pop back to 1st page with my new values passed in pop function.
to navigate from Page1 to Page2
usually you use Navigator.push()
for example:
// Page1
Navigator.push(context, MaterialPageRoute(builder: (context) => Page2()));
and when you are in Page2 you do some work for example saving some data in shared preference and go back to Page1 and refresh Page1 to get the new data from shared preferences, what you can do is to pop to Page1 using
//Page2
Navigator.pop(context);
this is not gonna be enough to refresh your Page1
so you need to attach a .then in the Page1 Navigator as a callback it will be called when you pop from page2 and that .then should have a set state to trigger a rebuild just call a something inside the set state to cause a rebuild
like this:
//Page1
Navigator.push(context, MaterialPageRoute(builder: (context) => Page2())).then((value) {
setState(() {
// refresh state of Page1
});
});
The question may be duplicate but I'm unable to found the solution. My app scenario is the same as the almost app. My first screen is Splash screen and holds for two seconds and here login session is checked and upon this condition screen changes like this and below code is run in initState()
_checkPreference() async {
PreferencesConnector myprefs= PreferencesConnector();
id=await myprefs.readString('merchantid');
if(id==null||id==''){
Future.delayed(
const Duration(seconds: 2),
() => Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => Login(),settings: RouteSettings(name: '/login')),
));
}else{
Future.delayed(
const Duration(seconds: 2),
() => Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => DashBoard()),
));
}
}
If the session returns false then it goes to login screen, In Login screen my scaffold inside in WillPopScope widget and that class is stateful class
return WillPopScope(
onWillPop: () {
if (Navigator.canPop(context)) {
//Navigator.pop(context);
Navigator.of(context).pop();
} else {
SystemNavigator.pop();
}
},
child:Scaffold(
body: Stack(
children: <Widget>[
and if LoginApi returns true then it move to dashboard like this
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => DashBoard(),
settings: RouteSettings(name: '/dashboard')),
);
here everything is working fine when the user is already logged-in and we reach to dashboard after the splash and but there is a logOut button on my dashboard when user press logout then there is a dialog appear which asks for logout- if i press yes from dialog button works like this
onPressed:(){
clearSession();
// Navigator.of(context).popUntil(ModalRoute.());
// Navigator.of(context).popUntil('/login', (Route<dynamic> route) => false);
// Navigator.popUntil(context, ModalRoute.withName("/login"));
// Navigator.of(context).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login()),);
},
after pressing logout it reaches to login screen where I'm unable to close the app when user press back from the device back in login screen but it redirects to the dashboard and then I pressed back then app closed.
What you need to do is first clear all path before going to login() screen.
try this:
onPressed:(){
clearSession();
//Navigator.popUntil(context, ModalRoute.withName('/'));
Navigator.pop(context,true);// It worked for me instead of above line
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login()),);
},
call this on your logout button
void _logout() {
Navigator.popUntil(context, ModalRoute.withName('/login'));
}
here is the link of official docs