使所有类别的数据都可用

问题描述 投票:0回答:1

我正在主页上创建http get request,该页面返回Iterable的列表。单击tab时,需要将此结果传递到屏幕。它无法正常运行,因为导航到新屏幕时返回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);
    }
  }
}
flutter dart tabs iterable
1个回答
0
投票

[您需要在有状态的小部件中设置setState来更改其状态并重建小部件,现在您只需传递在创建时分配的nullnewList值。

fetchIndex方法中,用newList包装setState分配代码,像这样

        setState((){
           newList = mentorIndexes.map((MentorIndex) => MentorIndex.mentee_id);
        })

通过这种方式,您将通知StatefulWidgetState对象已更改,需要重建。

© www.soinside.com 2019 - 2024. All rights reserved.