Flutter FutureBuilder Not Updating

喜欢而已 提交于 2021-01-28 00:12:08

问题


I have a Flutter FutureBuilder that needs to be updated with new data given by the user. However, the UI elements in the FutureBuilder do not update and still contain the old values. I have checked through print statements that the new data is correctly loaded. The issue seems to be with FutureBuilder rebuilding the widget when the new data is loaded. Any help is appreciated.

Future<List<PollItem>> fetchPost(String loc) async {
  return new Future(() async {

    final response = await http
        .post(restip + '/getPosts',
        body: {"data": loc});

    if (response.statusCode == 200) {
      print(response.body);

      // If the call to the server was successful, parse the JSON
      // This function adds json to list
      PollItem.fromJson(json.decode(response.body));

      // list is a list of posts gathered based on the string criteria
      return list;
    } else {
      throw Exception('Failed to load polls');
    }
  });
}

class PollState extends State<Poll> {
  TextEditingController textc = new TextEditingController();

  static String dropDowntext = "City";
  String _name = "Search";
  final _names = [''];


  Widget build(BuildContext context) {

    print("dropdown"+dropDowntext);
    textc.text = _name;
    print(dropDowntext);
    return FutureBuilder<List<PollItem>>(
      future: fetchPost(dropDowntext),
      initialData: [PollItem()],
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          print(snapshot.data[0].question);
          });
}

Here is my global file:

 List<PollItem> list = new List();
      factory PollItem.fromJson(Map<String, dynamic> json) {
        int len = json['length'];
        if(listNum!=len) {
          listNum = len;
          list.clear();
          for (int i = 0; i < len; i++) {
            list.add(PollItem(
              answer1: json[i.toString()]['answer1'],
              location: json[i.toString()]['location']

            )
            );
          }
        }
        }

回答1:


You don't need to create a Future object :

   Future<List<PollItem>> fetchPost(String loc) async {
    final response = await http.post(restip + '/getPosts',body: {"data": loc});

      if (response.statusCode == 200) {
        print(response.body);
        final data = json.decode(response.body);
        int len = data['length'];
        final List<PollItem> newList = List();
        for (int i = 0; i < len; i++) {
          newList.add(PollItem(
          answer1: data[i.toString()]['answer1'],
          location: data[i.toString()]['location']
            )
          );
        }

        print("new list size: ${newList.length}");

        return newList;
      } else {
        throw Exception('Failed to load polls');
      }
      return null;
  }


来源:https://stackoverflow.com/questions/54313596/flutter-futurebuilder-not-updating

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!