Сделать данные доступными для всех классов - PullRequest
0 голосов
/ 23 февраля 2020

Я делаю http get request на своей домашней странице, которая возвращает список Iterable. Мне нужно передать этот результат на экран при нажатии tab. Он не работает должным образом, поскольку возвращает ноль при переходе на новый экран. Так что я предполагаю, что это не правильный способ сделать это.

HomePage

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);
    }
  }
}

1 Ответ

0 голосов
/ 23 февраля 2020

Вам нужно установить setState в виджете с отслеживанием состояния, чтобы изменить его состояние и перестроить виджет, прямо сейчас вы просто передаете значение null newList, которое присваивается при создании.

В вашем fetchIndex method, оберните ваш newList код назначения с помощью setState, например,

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

Таким образом, вы уведомите ваш StatefulWidget, что его объект State изменился, и его нужно восстановить.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...