android – Flutter: FutureBuilder runs on another view-ThrowExceptions

Exception or error:

There is view A and view B. On A there is 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

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)

    ............
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());
                                });
                              }
                              ............
                        }),
                        ............
        });
  }
}
How to solve:

when you push from A to B you can implement it this way :

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

and when you pop from B to A:

Navigator.pop(context, some_function);

and then you can call some_function from A using:

result();

which you can use it in the FutureBuilder of A

if you want, share your code so we can help you to implement it or to find a better way.

Leave a Reply

Your email address will not be published. Required fields are marked *