Flutter 使数据可用于所有类别

Flutter 使数据可用于所有类别,flutter,dart,tabs,iterable,Flutter,Dart,Tabs,Iterable,我正在我的主页上发出一个httpget请求,该请求返回一个Iterable列表。单击选项卡时,我需要将此结果传递到屏幕。它不能像预期的那样工作,因为在导航到新的sscreen时返回null。所以我猜这不是正确的方法 主页 class DashBoardPage extends StatefulWidget { @override _DashBoardPageState createState() => _DashBoardPageState(); } class _DashBoa

我正在我的主页上发出一个
httpget请求
,该请求返回一个
Iterable
列表。单击
选项卡时,我需要将此结果传递到屏幕。它不能像预期的那样工作,因为在导航到新的sscreen时返回null。所以我猜这不是正确的方法

主页

class DashBoardPage extends StatefulWidget {
  @override
  _DashBoardPageState createState() => _DashBoardPageState();
}

class _DashBoardPageState extends State<DashBoardPage> {
  List<MentorIndex> mentorIndexes = [];
  SharedPreferences sharedPreferences;
  Iterable newList;

  Widget callPage(int currentIndex) {
    switch (currentIndex) {
      case 0:
        showTabs = true;

        return TabBarView(
            //trying to pass this list to new tab screen
            children: [new HomePage(), new SchedulePage(), RequestsPage(menteeIds: newList,)]);
        break;

      default:
        return HomePage();
    }
  }

  @override
  void initState() {
    super.initState();

    fetchIndex();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '',


  }



 }
   Future fetchIndex() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    var uri = NetworkUtils.host + AuthUtils.endPointIndex;
    try {
      final response = await http.get(
        uri,
        headers: {'Accept': 'application/json', 'Content-Type': 'application/json','Authorization': 'Bearer ' + sharedPreferences.get("token"), },
      );
      final responseJson = json.decode(response.body);
      for (var u in responseJson["data"]) {
        MentorIndex user = MentorIndex(
            u["id"],
            u["mentor_id"],
            u["mentee_id"],
            u["status"],
            u["session_count"],
            u["current_job"],
            u["email"],
            u["phone_call"],
            u["video_call"],
            u["face_to_face"],
            u["created_at"],
            u["updated_at"]);

        mentorIndexes.add(user);
        newList = mentorIndexes.map((MentorIndex) => MentorIndex.mentee_id);
      }
      return responseJson;
    } catch (exception) {
      print(exception);
    }
  }
}
class DashBoardPage扩展StatefulWidget{
@凌驾
_DashBoardPageState createState()=>DashBoardPageState();
}
类_DashBoardPageState扩展状态{
列表索引=[];
SharedReferences SharedReferences;
可编辑的新列表;
小部件调用页(int currentIndex){
开关(当前索引){
案例0:
showtab=true;
返回选项卡视图(
//正在尝试将此列表传递到新选项卡屏幕
子项:[新主页(),新日程页(),请求页面(menteeid:newList,)];
打破
违约:
返回主页();
}
}
@凌驾
void initState(){
super.initState();
fetchIndex();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“”,
}
}
Future fetchIndex()异步{
SharedReferences SharedReferences=等待SharedReferences.getInstance();
var uri=NetworkUtils.host+AuthUtils.endPointIndex;
试一试{
最终响应=等待http.get(
乌里,
标题:{'Accept':'application/json','Content Type':'application/json','Authorization':'Bearer'+SharedReferences.get(“令牌”),},
);
final responseJson=json.decode(response.body);
对于(var u在responseJson[“数据”]){
导师索引用户=导师索引(
u[“id”],
u[“导师id”],
u[“学员id”],
u[“地位”],
u[“会话计数”],
u[“当前工作”],
u[“电子邮件”],
u[“电话”],
u[“视频通话”],
u[“面对面”],
u[“创建于”],
u[“更新于”];
添加(用户);
newList=MentorIndex.map((MentorIndex)=>MentorIndex.mentee_id);
}
返回响应;
}捕获(例外){
打印(例外);
}
}
}

您需要在有状态小部件中设置state以更改其状态并重建小部件,现在您只需传递创建时分配的
newList
null

fetchIndex
方法中,用
setState
newList
分配代码包装起来,如下所示

setState(){
newList=MentorIndex.map((MentorIndex)=>MentorIndex.mentee_id);
})
这样,您将通知您的
StatefulWidget
State
对象已更改,需要重新生成