颤振"Could not find the correct Provider<UserProvider> above this PickupLayout Widget"



我得到错误"无法在此PickupLayout小部件上方找到正确的提供者";当我使用导航时。推送一个showDialog查询。没有showDialog弹出窗口,它可以工作

...
Padding(
padding: const EdgeInsets.only(right: 15),
child: IconButton(
icon:Icon(Icons.check),
nPressed: () {
query_popup();   // using navigator.push at this position works
},
),
),
...
query_popup() {      
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(8.0),
),
),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(height: 5.0),
Text("End & save?"),
Container(height: 20.0),
InkWell(                        // using navigator.push at this posistion doesn't work
onTap: () {
Navigator.push(context,MaterialPageRoute(builder: (context) => TabbarScreen(SelectedPage: 0)),);
},
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(width: 10.0, height: 40.0),
Text('Yes'),
],
),
),
Container(height: 5.0),
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(width: 20.0, height: 40.0),
Text('No')
],
),
),
],
),
);
},
);
}
}

完整的错误描述:错误:无法在此PickupLayout小部件上方找到正确的提供程序发生这种情况可能是因为您使用的BuildContext不包括提供程序随你的便。有几个常见的场景:

  • 您正在尝试读取的提供程序在不同的路由中。提供程序是有"作用域"的。如果你在路由中插入了ofprovider,那么其他路由将无法访问该提供商。
  • 您使用的BuildContext是您试图读取的提供者的祖先。确保PickupLayout在你的MultiProvider/Provider下。这通常发生在你创建一个提供商并试图立即读取它时。

将TabbarScreen与Provider工作过的标签包起来,谢谢@ikerfah