首页 > 解决方案 > Flutter:在同一页面中选择过滤器时刷新列表 API

问题描述

我在 Flutter 中有一个用例,我需要在其中显示来自 API 的列表。在页面中选择特定过滤器后,应通过触发新 API 来刷新显示的列表。下面是我的源代码,我在其中显示来自 API 的列表。

class _MyFieldReportForm extends State<MyFieldReport> {
  var myContext;

  @override
  Widget build(BuildContext context) {
    myContext = context;
    // TODO: implement build
    return Scaffold(
      body: new FutureBuilder<List<ReportData>>(
        future: fetchProducts(new http.Client()),
        builder: (context, snapshot) {
          if (snapshot.hasError) print(snapshot.error);

          return snapshot.hasData
              ? createListView(snapshot, myContext)
              : new Center(child: new CircularProgressIndicator());
        },
      ),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.add),
        backgroundColor: Colors.green,
        onPressed: () {
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => FieldvisitForm()));
        },
      ),
    );
  }
}

按下任何过滤器按钮后,我需要用新的 API 刷新此 API。如果有人可以帮助我提供示例代码,那就太好了。

标签: flutter

解决方案


终于知道解决办法了。可以通过获取列表来完成

void initState() {
super.initState();
listFuture = fetchProducts(new http.Client());

}

在 setState 中,我正在更新列表。下面是完整的代码:

     Future<List<ReportData>> listFuture;
     body: new FutureBuilder<List<ReportData>>(
        future: listFuture,
        builder: (context, snapshot) {
          if (snapshot.hasError) print(snapshot.error);

          return snapshot.hasData
              ? createListView(snapshot, myContext)
              : new Center(child: new CircularProgressIndicator());
        },
      ),

onPressed: () {
          setState(() {
            refreshList();
          });

void refreshList() {
    listFuture = fetchProductsUpdate(new http.Client());
  }

推荐阅读