未来构建器中毫无意义地请求 Api 的躁动
时间:2024-03-24
浏览:327
作者:佚名
在 Flutter 应用程序中,FutureBuilder 组件用于显示异步获取的数据。然而,在编辑对话框中,当单击表单字段时,FutureBuilder 会频繁地重复执行获取请求,导致不必要的服务器请求。这是由于键盘出现和消失时底层小部件重建,从而触发了 API 调用。
我的 flutter 应用程序中有一个 future builder,它显示 --
- 错误:如果 json 解析出现错误
- 数据:如果一切顺利
- 加载器:是否需要时间
一切正常。 future 正在调用一个“future”函数,该函数正在执行一些学生数据的获取请求,并且“builder”正在显示它。我在同一页面上有一个编辑对话框。我可以通过 put 请求编辑学生信息。问题是,当我单击编辑对话框中的表单字段时,我注意到 get 请求自动发生大约 10 次。当我保存编辑时,会出现一个确认对话框,表明数据已更新。当这种情况再次发生时,获取请求最多会发生 10 次。然后它就会弹出。所以服务器上大约有 20 个无用的请求。 我认为发生这种情况是因为当我单击表单字段时,键盘会出现,底层显示小部件会重建,调用 api。当数据被编辑时,键盘会再次回到原来的位置,小部件会重建,调用 api。我该如何解决这个问题?
如果有帮助的话,这是代码:
child: FutureBuilder(
future: APIs().getStudentDetails(),
builder: (context, data) {
if (data.hasError) {
return Padding(
padding: const EdgeInsets.all(8),
child: Center(child: Text("${data.error}")));
} else if (data.hasData) {
var studentData = data.data as List<StudentDetails>;
return Padding(
padding: const EdgeInsets.fromLTRB(0, 15, 0, 0),
child: SingleChildScrollView(
child: SizedBox(
height: MediaQuery.of(context).size.height * 0.9,
child: ListView.builder(
itemCount: studentData.length,
itemBuilder: ((context, index) {
final student = studentData[index];
final id = student.studentId;
final father = student.fatherName;
final mother = student.motherName;
final cg = student.cg;
final cityName = student.city;
final studentName = student.studentName;
return SizedBox(
child: Padding(
padding: const EdgeInsets.all(30.0),
child: SingleChildScrollView(
child: GestureDetector(
onDoubleTap: () {
edit(context, id!, studentName!, father,
mother, cg, cityName!);
},
child: Column(children: [
CustomReadOnlyField(
hintText: id.toString()),
CustomReadOnlyField(hintText: studentName),
CustomReadOnlyField(hintText: father),
CustomReadOnlyField(hintText: mother),
CustomReadOnlyField(
hintText: cg.toString()),
CustomReadOnlyField(hintText: cityName),
]),
),
),
),
);
}),
scrollDirection: Axis.vertical,
),
),
),
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),正确答案
我遵循了这个答案,它起作用了。 Flutter FutureBuilder gets constantly called 显然我必须“延迟初始化我的未来”和“在 initState 中初始化我的未来:”
本篇关于《未来构建器中毫无意义地请求 Api 的躁动》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
