![](/img/trans.png)
[英]Flutter FutureBuilder not triggered when onPressed of ElevatedButton runs
[英]Flutter: FutureBuilder runs on another view
有視圖 A 和視圖 B。在 A 上有 FutureBuilder。 當我通過 Navigator.Push() 從視圖 A 切換到視圖 B 時,會激活一個附加到視圖 A 上的 FutureBuilder 的函數。如果我不在 Futurebuilder 視圖中,我不需要 FutureBuilder 工作
這是"view A"(_StoriesState)的代碼,當我去B(StoryPage)時調用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 中使用它
如果您願意,請分享您的代碼,以便我們幫助您實現它或找到更好的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.