Error thrown on navigator pop until : "!_debugLocked is not true - android

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

Related

Getx did not clear the previous views after doing log out

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

Refresh page when user come back from another page, Navigate using Drawer menu

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

How to do a Flutter Test for a widget that accesses Provider.of(context)

I was wondering if someone could help me out, I am having trouble writing tests for my home page on my flutter project as it relies on a Provider.
home_page.dart
Future<NewsModel> newsData = Provider.of<Future<NewsModel>>(context);
return FutureProvider(
create: (context) => newsData,
child: (newsData != null)
? Scaffold(
body: SmartRefresher(
controller: _refreshController,
onRefresh: _refreshNews,
onLoading: _onLoading,
child: Consumer<NewsModel>(
builder: (_, news, __) {...}
main.dart
return MultiProvider(
providers: [
FutureProvider(create: (context) => geolocatorService.getlocation()),
ProxyProvider<Position, Future<NewsModel>>(
update: (context, position, news) {
return (position != null) ? newsService.getNews() : null;
},
),
home_page_test.dart
testWidgets('Test the home screen', (WidgetTester tester) async {
HomePage homePage = new HomePage();
await tester.pumpWidget(
makeWidgetTestable(
child: homePage,
),
);
});
}
When I run this test I receive the below error, I am really confused here and need some guidance. I am fairly experienced with Flutter but am a newbie to writing widget tests for widgets that require external dependencies. If someone can help this would be great.
Error:
The following ProviderNotFoundException was thrown building HomePage(dirty, dependencies [MediaQuery], state: _HomePageState#ce244): Error: Could not find the correct Provider<Future<NewsModel>> above this HomePage Widget
If you wrap the following code with the provider from your main file:
await tester.pumpWidget(
makeWidgetTestable(
child: homePage,
),
);
and turn it into:
testWidgets('Test the home screen', (WidgetTester tester) async {
//No longer need the following line
//HomePage homePage = new HomePage();
await tester.pumpWidget(
MultiProvider(
providers: [
FutureProvider(create: (context) => geolocatorService.getlocation()),
ProxyProvider<Position, Future<NewsModel>>(
update: (context, position, news) {
return (position != null) ? newsService.getNews() : null;
},
),
child: homePage,
),
);

close app on device back button in flutter

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

How to go back from web view in flutter?

After clicking the button it should show the web view. I did this. But when press the back button it is closing the app. It does not go back to previous state. If I press back button it should go back to previous page or state. I am getting web view from a function.
Here is my code:
_contentSearch(BuildContext context){
url = "https://www.google.com";
return WebviewScaffold(
url: url,
appBar: AppBar(
title: Text("Google"),
),
withJavascript: true,
withLocalStorage: true,
withZoom: false,
enableAppScheme: true,
)
},
);
}
And I am calling this method like this:
FlatButton(
padding: EdgeInsets.all(0.0),
onPressed: (){
Navigator.pushReplacement(
context,
new MaterialPageRoute(builder: (BuildContext context) => _contentSearch(context)
));
},
child: new Text("Button"),
),
It's not going back. It's always closing the app.
Its because you are using pushReplacement(Replace the current route of the navigator) instead of push
Navigator.push(
context,
MaterialPageRoute(builder: (context) => _contentSearch(context),
);

Categories

Resources