Root widget not rebuilding when popping from child

Viewed 559

I have been working on a flutter app where the user starts on a stateful widget with a ListView of items from a SQLite database. The user can tap on an item in the list which navigates to a page where the item can be modified and saved.

When Navigator.pop(context) is used, the app returns to the ListView but doesn't rebuild. The changes made do not show until I force a rebuild (hot reload)

This is a new issue in flutter 1.17.

Root view

class ItemsView extends StatefulWidget {
  @override
  _ItemsView State createState() => _ItemsViewState();
}

class _ItemsViewState extends State<ItemsView> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<Story>>(
        future: DBProvider.db.getAllItems(),
        builder: (BuildContext context, AsyncSnapshot<List<Story>> snapshot) {
          if (snapshot.hasData) {
            return Text(snapshot.data[0])
          }
        }
      )
    }
  } 

Second View

class ModifyView extends StatefulWidget {
  @override
  _ModifyView State createState() => _ItemsViewState();
}

class _ItemsViewState extends State<ItemsView> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<Story>>(
        future: DBProvider.db.getAllItems(),
        builder: (BuildContext context, AsyncSnapshot<List<Story>> snapshot) {
          if (snapshot.hasData) {
            return Text(snapshot.data[0])
          }
        }
      )
    }
  } 

How can I force the widget to reload?

1 Answers

You can use the Navigator's method then on which you can reload the page or do any other stuff. In below example I am using the screen A to to screen B navigation and When user navigate the from the B to A , we will refresh the view or do any other stuff like below.

From Screen A -> B

Navigator.push(
          context,
          MaterialPageRoute(
            settings: RouteSettings(
                name: B), ///// HERE "B" IS THE CLASS NAME
            builder: (context) =>
                B(),
          ),
        ).then((value) {
          //// THIS METHOD IS ENVOKE WHEN SCREEN COME FROM  B->A, YOU CAN PERFROM CAN TASK HERE
        });

Inside the B screen, we need to create the constructor like below

class B extends StatefulWidget {

  B () ;

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _B();
  }
}

Navigation from B to A

Navigator.pop(context, 1); //// HERE WE ARE PUSHING THE ANY VALUE "1" FOR THE RETURN IN then OF CLASS "A"
Related