android - Flutter:FutureBuilder 在另一个视图上运行
问题描述
有视图 A 和视图 B。在 A 上有 FutureBuilder。当我通过 Navigator.Push() 从视图 A 切换到视图 B 时,会激活一个附加到视图 A 上的 FutureBuilder 的函数。如果我不在 Futurebuilder 的视图中,我不需要 FutureBuilder 工作
这是“视图A”(_StoriesState)的代码,当我转到B(StoriesPage)时,会调用getStories函数,该函数只能在A(_StoriesState)中调用
............
Future<dynamic> getStories(int items, List<int> pickerSelectedIndex) async {
try {
............
return stories;
} on HandshakeException catch (e) {
print("HE: " + e.toString());
getStories(items, pickerSelectedIndex);
} on SocketException catch (e) {
print("SE: " + e.toString());
getStories(items, pickerSelectedIndex);
} on Exception catch (e) {
print(e);
getStories(items, pickerSelectedIndex);
}
}
............
class _StoriesState extends State<Stories> {
Future<List<Story>> listViewData;
............
Future<List<Story>> getStoriesDataReady(
int items, List<int> pickerSelectedIndex) async {
List<Story> stories = await getStories(items, pickerSelectedIndex);
if (globals.filter.isEmpty) {
return stories;
} else {
............
return searchStories;
}
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: ............
builder: (BuildContext context, AsyncSnapshot snapshot) {
List<Widget> children;
if (snapshot.hasData) {
listViewData =
getStoriesDataReady(globals.items, pickerSelectedIndex);
return Scaffold(
body: FutureBuilder<List<Story>>(
future: listViewData,
builder: (BuildContext context,
AsyncSnapshot<List<Story>> snapshot) {
if (snapshot.hasData) {
............
child: FloatingSearchBar.builder(
............
itemCount: snapshot.data.length + 1,
itemBuilder:
(BuildContext context, int index) {
............
onTap: () {
............
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => StoryPage(............)),
).then((value) {
FocusScope.of(context).requestFocus(FocusNode());
});
}
............
}),
............
});
}
}
解决方案
当你从 A 推到 B 时,你可以这样实现:
Function result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => B()),
);
当你从 B 跳到 A 时:
Navigator.pop(context, some_function);
然后您可以使用以下命令从 A 调用 some_function:
result();
您可以在 A 的 FutureBuilder 中使用它
如果您愿意,请分享您的代码,以便我们帮助您实现它或找到更好的方法。
推荐阅读
- javascript - 如果第一张或最后一张幻灯片处于活动状态,则禁用 centerMode - Slick Slider
- python - 从 URL 读取 Zip 文件并将 Ms Access 文件转换为数据框
- python - 将 gdal geotiff 导出到 MySQL
- bash - 使用 openssl 验证文本文件中的服务器
- python - Python selenium:在输入框中输入日期
- kubernetes - 将现有的 kubernetes 添加到 gitlab 会引发错误
- eigen - 取决于操作顺序的结果差异很大
- android - listFiles() 在 Android 10(范围存储)中没有用。需要特定目录的文件
- c# - 为什么我的复选框或单选按钮没有显示在网站上?
- mongodb - MongoDB watch() 返回 JSON