简体   繁体   English

Flutter:FutureBuilder 在另一个视图上运行

[英]Flutter: FutureBuilder runs on another view

There is view A and view B. On A there is FutureBuilder.有视图 A 和视图 B。在 A 上有 FutureBuilder。 When I switch from view A to view B via Navigator.Push(), a function is activated that is attached to FutureBuilder on view A. I do not need FutureBuilder to work if I'm not on view with Futurebuilder当我通过 Navigator.Push() 从视图 A 切换到视图 B 时,会激活一个附加到视图 A 上的 FutureBuilder 的函数。如果我不在 Futurebuilder 视图中,我不需要 FutureBuilder 工作

This is the code of "view A" (_ StoriesState), when I go to B (StoryPage) the getStories function is called which can only be called in A (_StoriesState)这是"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());
                                });
                              }
                              ............
                        }),
                        ............
        });
  }
}

when you push from A to B you can implement it this way :当您从 A 推送到 B 时,您可以通过以下方式实现:

Function result = await Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => B()),
);

and when you pop from B to A:当你从 B 跳到 A 时:

Navigator.pop(context, some_function);

and then you can call some_function from A using:然后您可以使用以下方法从 A 调用 some_function:

result();

which you can use it in the FutureBuilder of A您可以在 A 的 FutureBuilder 中使用它

if you want, share your code so we can help you to implement it or to find a better way.如果您愿意,请分享您的代码,以便我们帮助您实现它或找到更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM