我尝试将上下文传递给第二个小部件树(在函数中),但出现错误:
尝试将 Provider 与 Listenable/Stream (Model2) 的子类型一起使用。这可能是一个错误,因为 Provider 不会在 Model2 更新时自动更新依赖项。相反,请考虑将 Provider 更改为处理更新机制的更具体的实现,例如: - ListenableProvider - ChangeNotifierProvider - ValueListenableProvider - StreamProvider
Future<void> _neverSatisfied({Key key, @required BuildContext 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('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();
},
),
],
),
);
},
);
}
相同的第一个小部件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, _) =>
第二(我从这里传递上下文):
@override
Widget buildStep(BuildContext context) {
Consumer<Model2>(
builder: (context, model2, _) =>
...
_neverSatisfied(context: context); //**pass context**