如何将上下文传递给提供者的第二个小部件树

时间:2019-08-20 19:45:15

标签: flutter dart flutter-provider

我尝试将上下文传递到第二个小部件树(在功能上),但出现错误:

  

试图将Provider与侦听/流(模型2)的子类型一起使用。   这可能是一个错误,因为提供程序不会自动更新   依赖于Model2更新时。相反,请考虑更改提供者   对于处理更新机制的更具体的实现,   如:   -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**

1 个答案:

答案 0 :(得分:0)

您应该使用Provider.value代替ChangeNotifierProvider.value