I need pass provider (Model2
) from homepage
to Page2
so when user go back to homepage
(onWillPop
) I can make API call from provider (Model2
) and update homepage
.
But when I call _onPaidBackPress(context)
there is error:
Unhandled Exception: Error: Could not find the correct Provider above this Consumer Widget
StatefulWidget
in homepage:
@override
Widget build(BuildContext context) {
return ChangeNotifierProxyProvider<Model1, Model2>(
initialBuilder: (_) => Model2(),
builder: (_, model1, model2) => model2
..string = model1.string,
),
child: Consumer<Model2>(
builder: (context, model2, _) =>
...
await Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) =>
new Page2(
context: context)));
In Page2:
class Page2 extends StatefulWidget {
final BuildContext context;
Page2({Key key, this.context}) : super(key: key);
@override
State createState() => new Page2State(context: context);
}
class Page2State extends State<Page2> {
final context;
ChatScreenState({Key key, this.context});
@override
Widget build(BuildContext context) {
return Consumer<Model1>(
builder: (context, model, _) {
return new WillPopScope(
onWillPop: () => model.isPaid ? _onPaidBackPress(context) : _onBackPressed(context),
Future<void> _onPaidBackPress(context) async {
final model2 = Provider.of<Model2>(context, listen: false);
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return
Provider.value(value: model2, child:
AlertDialog(
title: Text('Back'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Go back'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: () async {
await model2.getData();
Navigator.of(context).pop();
},
),
],
),
);
},
);
}
Thanks for help!
ListenableProvider is the specific provider used for listenable objects. It will listen, then ask widgets depending on it and affected by the state change to rebuild any time the listener is called. ChangeNotifierProvider is similar to ListenableProvider but for ChangeNotifier objects, and calls ChangeNotifier.
For using the provider package we need to add the provider package to the dependencies section of pubspec. yaml and click on the get button to get the dependencies. First of all, we will be defining a model library inside of the lib folder which consists of item. dart and item_data.
MaterialPageRoute<T> class Null safety. A modal route that replaces the entire screen with a platform-adaptive transition. For Android, the entrance transition for the page zooms in and fades in while the exiting page zooms out and fades out. The exit transition is similar, but in reverse.
You can easily pass the provider in the Navigator by creating a new provider when Navigating to a new page.
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) => Provider(
create: (context) => InventoryItem(),
builder: (context, child) => ProductDetails(),
),
),
);
Here either create a new object InventoryItem()
or pass the existing provider object.
I am a bit late but I found a solution on how to keep the value of a Provider
alive after a Navigator.push()
without having to put the Provider
above the MaterialApp
.
To do so, I have used the library custom_navigator
. It allows you to create a Navigator
wherever you want in the tree.
You will have to create 2 different GlobalKey<NavigatorState>
that you will give to the MaterialApp
and CustomNavigator
widgets. These keys will allow you to control what Navigator you want to use.
Here is a small snippet to illustrate how to do
class App extends StatelessWidget {
GlobalKey<NavigatorState> _mainNavigatorKey = GlobalKey<NavigatorState>(); // You need to create this key for the MaterialApp too
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: _mainNavigatorKey; // Give the main key to the MaterialApp
home: Provider<bool>.value(
value: myProviderFunction(),
child: Home(),
),
);
}
}
class Home extends StatelessWidget {
GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>(); // You need to create this key to control what navigator you want to use
@override
Widget build(BuildContext context) {
final bool myBool = Provider.of<bool>(context);
return CustomNavigator (
// CustomNavigator is from the library 'custom_navigator'
navigatorKey: _navigatorKey, // Give the second key to your CustomNavigator
pageRoute: PageRoutes.materialPageRoute,
home: Scaffold(
body: FlatButton(
child: Text('Push'),
onPressed: () {
_navigatorKey.currentState.push( // <- Where the magic happens
MaterialPageRoute(
builder: (context) => SecondHome(),
),
},
),
),
),
);
}
}
class SecondHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
final bool myBool = Provider.of<bool>(context);
return Scaffold(
body: FlatButton(
child: Text('Pop'),
onPressed: () {
Novigator.pop(context);
},
),
);
}
}
Here you can read the value myBool
from the Provider
in the Home
widget but also ine the SecondHome
widget even after a Navigator.push()
.
However, the Android back
button will trigger a Navigator.pop()
from the Navigator of the MaterialApp
. If you want to use the CustomNavigator
's one, you can do this:
// In the Home Widget insert this
...
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
if (_navigatorKey.currentState.canPop()) {
_navigatorKey.currentState.pop(); // Use the custom navigator when available
return false; // Don't pop the main navigator
} else {
return true; // There is nothing to pop in the custom navigator anymore, use the main one
}
},
child: CustomNavigator(...),
);
}
...
You would need to make the Provider appear above the Navigator Widget to be an anchestor of the current Widget. That means, it must reside above the MaterialApp()
, CupertinoApp()
or whatever Directionality()
you might use.
Please refer to following links for more information about InheritedWidget.
https://medium.com/@mehmetf_71205/inheriting-widgets-b7ac56dbbeb1
https://www.youtube.com/watch?v=Zbm3hjPjQMk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With