首页 > 解决方案 > 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()

谁能看到问题出在哪里,我没有收到任何错误,但代码不起作用

标签: flutterprovider

解决方案


推荐阅读