2

我在应用程序页面上初始化Provider

@override
  Widget build(BuildContext context) {
return ChangeNotifierProxyProvider<Model1, Model2>(
initialBuilder: (_) => Model2(),
  builder: (_, model1, model2) => model2
    ..string = model1.string,
),
  child: Consumer<Model2>(
    builder: (context, model2, _) =>

...
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => Page2(context: context)),

但是当我将 Navigator.push 页面推送到 Page2 时:

class Page2 extends StatelessWidget {

final context;

  Page2({Key key, this.context});
...

@override
Widget build(BuildContext context) {

return Consumer<Model2>(
    builder: (context, model2, _) =>
MaterialApp(
home: Scaffold(
…

它给出错误:

错误:在此消费者小部件上方找不到正确的提供者

我该如何解决这个错误?

4

0 回答 0