Tony
Tony

Reputation: 2748

Refresh data in ListView flutter

I have a menu page which has tabA and tabB. The appBar has a search button.

menu.dart

   @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Item List'),
        actions: [
          _currentIndex == 0
              ? Row(
                  children: [
                    IconButton(
                        onPressed: () {
                            Navigator.pushNamed(context, SearchScreen.ROUTE,
                                arguments: list);                        
                        },
                        icon: Icon(
                          Icons.search,
                          color: Colors.white,
                        )),
                    IconButton(
                      icon: Icon(
                        Icons.tune,
                        color: Colors.white,
                      ),
                      onPressed: () {
                            ...
                            }
                          }
                        });
                      },
                    )
                  ],
                )
              : Container()
        ],
        bottom: TabBar(
          unselectedLabelColor: Colors.white,
          labelColor: Colors.white,
          isScrollable: false,
          tabs: <Widget>[
            Tab(
              text: "Tab A",
            ),
            Tab(
              text: "Tab B",
            ),
          ],
          controller: _tabController,
          indicatorColor: Colors.white,
          indicatorSize: TabBarIndicatorSize.tab,
        ),
        bottomOpacity: 1,
      ),
      body: TabBarView(
        controller: _tabController,
        children: <Widget>[
          ShowTabA(),
          ShowTabB()
        ],
      ),
    );
  }
}

In tabA, it has a listView. When I tap on one of the listview item, it will navigate to EditPage. When return from EditPage, I will refresh the data in tabA. All the list item will be updated.

tabA.page

initState(){
    _bloc.getItemList();
}

Navigator.pushNamed(EditPage.ROUTE,context).then((onValue){
 _bloc.getItemList();
});

My problem here:

When I click the search icon in menu page , it will navigate to SearchScreen page.Once user click the list item in SearchScreen, it will navigate to EditPage.

When back from EditPage, I want the list in TabA page refreshed but it can't because I navigate using pushReplacementNamed in menu page . How to achieve that ?

SearchScreen.dart

Navigator.pushReplacementNamed(context);

Upvotes: 1

Views: 1937

Answers (1)

Midhun MP
Midhun MP

Reputation: 107131

Updated answer on Modified Question:

Since you are using pushReplacement your await won't work. You can use the following approach to make it work:

First you have to define your routes:

routes: <String, WidgetBuilder> {
   '/': (BuildContext context) => // Your MainPage,
   '/menu': (BuildContext context) => MenuPage(),
   '/search': (BuildContext context) => SearchPage(),
   '/edit': (BuildContext context) => EditPage(),
}

Navigation from Menu to Search:

void goToSearch() async {

   await Navigator.pushNamed(context, "/search", arguments: list);
   _bloc.getItemList().then((onValue){
      setState(() {
         list = onValue;
      });
   });

}

Use pushNamed instead of pushReplacementNamed when navigating from Search to Edit:

Navigator.of(context).pushNamed("/edit");

Important step When you have to go back to Menu from Edit, you've to use popUntil method:

Navigator.popUntil(context, ModalRoute.withName('/menu'));

Upvotes: 1

Related Questions