flutter - 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。如果有人可以帮助我提供示例代码,那就太好了。
解决方案
终于知道解决办法了。可以通过获取列表来完成
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());
}
推荐阅读
- django - ASGI 框架生命周期错误,在没有生命周期支持的情况下继续
- python - Numpy Kronecker delta 矩阵
- pandas - 在 Pandas Dataframe 中的两列上进行分组,其中一列上有 bin(范围)
- mongodb - 如何将字符串转换为 BSON
- bulk - ORACLE:如何在一个查询中批量收集主数据和详细信息?
- javascript - 如何避免元标记在 php 中运行
- mysql - SQL 使用 COUNT(*) AS CountOf 插入选择
- node.js - 构建一次性密码认证服务器
- javascript - 有没有办法从表单中获取值数组
- python - 从连续元素之间的差异小于特定数字的列表创建嵌套列表