我在应用页面上初始化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(
…
出现错误:
错误:在此上方找不到正确的提供者 消费者小工具
如何解决此错误?