Flutter FutureBuilder未更新 [英] Flutter FutureBuilder Not Updating

查看:57
本文介绍了Flutter FutureBuilder未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flutter FutureBuilder,需要使用用户提供的新数据进行更新.但是,FutureBuilder中的UI元素不会更新,仍然包含旧值.我已经通过打印语句检查了新数据是否正确加载.问题似乎与FutureBuilder在加载新数据时重建小部件有关.任何帮助表示赞赏.

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

这是我的全局文件:

 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']

            )
            );
          }
        }
        }

推荐答案

您不需要创建 Future 对象:

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

这篇关于Flutter FutureBuilder未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆