I am try follow this answer for add provider to second widget tree (so can call provider from function). But I get error:
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: Error: Could not find the correct Provider above this Page Widget
For sure I provide Model2
correct in other widget tree so it have ancestor.
Future<void> _neverSatisfied() 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('Rewind and remember'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('You will never be satisfied.'),
Text('You\’re like me. I’m never satisfied.'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () async {
await model2.getData();
Navigator.of(context).pop();
},
),
],
),
);
},
);
}
First widget in same StatefulWidget
:
@override
Widget build(BuildContext context) {
return ChangeNotifierProxyProvider<Model1, Model2>(
initialBuilder: (_) => Model2(),
builder: (_, model1, model2) => model2
..string = model1.string,
),
child: Consumer<Model2>(
builder: (context, model2, _) =>
Second:
@override
Widget buildStep(BuildContext context) {
Consumer<Model2>(
builder: (context, model2, _) =>
...
_neverSatisfied();