我想在值从null更改为数字时通知包装器,但包装器似乎没有接收到更改。检查我下面的代码:
class setID with ChangeNotifier{
String studyID;
void setStudyID(String study){
studyID = study;
notifyListeners();
}
}在main.dart中,
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => setID()),
// others providers here
],
child: MaterialApp(
home: Wrapper(),
),在wrapper.dart中,
final studyID = Provider.of<setID>(context).studyID;当setID类中的值发生更改时,包装器中不会收到任何值。有谁能帮帮我吗?提前谢谢你。
发布于 2021-02-28 11:51:24
只需将代码从
providers: [
ChangeNotifierProvider(create: (_) => setID()),
// others providers here
],致,
providers: [
ChangeNotifierProvider<setID>(create: (_) => setID()),
// others providers here
],在wrapper中获取提供程序侦听器为
final studyID = Provider.of<setID>(context);这里的问题是ChangeNotifierProvider不知道要监视哪个变量/类对象。它应该能做到这一点。有关更多信息,请阅读Providers的官方文档
https://stackoverflow.com/questions/66405065
复制相似问题