flutter - Flutter 无法使用提供程序在另一个页面上获取值
问题描述
我正在使用提供程序包将从 API 调用中获得的数据传输到不同的页面。我的用例是在获得 API 响应后,数据应该被发送到不同的其他页面。问题是当我点击通向其他页面的按钮时,什么也没有发生。但是,当我删除我的提供程序实例时,该按钮可以工作并且能够继续到下一页,但没有来自我的 API 调用的数据。
一些代码供参考
//Code that is supposed to supply values received from API call
if (response.statusCode==200){
jsonData= json.decode(response.body);
setState(() {
sharedPreferences.setString("token", jsonData["token"]);
sharedPreferences.setInt("rvcid", jsonData["rvc_id"]);
userRvcs=jsonData["user_rvcs"];
});
Provider.of<userRvcDdata>(context).updateRvc(userRvcs);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
Dashboard(userRvcs: userRvcs,)
));
}
//My provider class
class userRvcDdata extends ChangeNotifier{
List userRvcs=[];
void updateRvc(List newRvc){
userRvcs=newRvc;
notifyListeners();
}
getuserRvcs() {
return userRvcs;
}
}
//My main.dart provider section
Widget build(BuildContext context) {
return ChangeNotifierProvider<userRvcDdata>(
create: (context)=> userRvcDdata(),
child: MaterialApp()
//How am trying to use the provider values in the other page (not Dashboard())
List received=Provider.of<userRvcDdata>(context).gettaskrad()
谁能看到问题出在哪里,我没有收到任何错误,但代码不起作用
解决方案
推荐阅读
- python - 无法在 Mac 上将 GLFW3 与 python 一起使用
- git - 无法删除 git 凭据
- sql - 如何在postgres中对数组类型进行内连接?
- jasper-reports - 报告。子报表未出现在应用程序中
- r - 如何在while循环中运行多个数字以查找GCD
- c# - 有没有办法在 c# 中获取复杂枚举的名称?
- php - 如何使用 PHPMailer 附加 .eml 文件?
- r - 命名空间 'rlang' 0.4.9 正在加载,但 >= 0.4.10 是必需的
- python - 如何在 lambda 层中读取 .pem 文件?
- python - 从 CSV 下载创建有吸引力的 HTML 报告