Provider not updating when doing, Navigator.pop(context) in flutter

Viewed 333

Here I am using provider package for state management.
I have a SpeedDial widget in the floatingActionButton. And whenever I add something in the list mainGoalList by using speedDial and do Navigator.pop(context); it does go back to the page but does not update the list.

SpeedDial

        SpeedDialChild(
                  child: Icon(Icons.book),
                  backgroundColor: Colors.blue,
                  label: 'Add Notes',
                  labelStyle: TextStyle(
                    fontSize: 18.0,
                    color: Colors.black,
                  ),
                  onTap: () {
                    showModalBottomSheet(
                        context: context,
                        isScrollControlled: true,
                        builder: (context) => SingleChildScrollView(
                              child: Container(
                                padding: EdgeInsets.only(
                                    bottom: MediaQuery.of(context)
                                        .viewInsets
                                        .bottom),
                                child: AddNotes(),
                              ),
                            ));
                  }),
 

AddNote

          Center(
            child: FlatButton(
              onPressed: () {
                if (_actcontroller.text == null) {
                  print("Cannot add null topic");
                } else {
                  addingTheNotes();
                  Navigator.pop(context);
                }
              },
              child: Text("Add"),
              color: Colors.blue,
            ),
          )
 

Adding the notes

  addingTheNotes() {
    theDataProvider.ourAllnotes.add(
      TodaysNoteClass(
        note: _actcontroller.text,
        dateTime: theDataProvider.notesChoosenDate,
        status: false,
      ),
    );
    theDataProvider.showingTheTodaysList();
  }

Here is the change notifier

  List<TodaysNoteClass> _ourAllnotes = [];
  List<TodaysNoteClass> get ourAllnotes => _ourAllnotes;
  set ourAllnotes(List<TodaysNoteClass> val) {
    _ourAllnotes = val;
    notifyListeners();
  }

Consumer class, this is where I am showing the notes

class HomePage extends StatefulWidget {
  static const String id = 'homePage';
  final String todaysDate =
      DateFormat('d MMMM').format(DateTime.now()).toLowerCase();
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  var theDataProvider;

  Widget build(BuildContext context) {
    theDataProvider = Provider.of<TheData>(context, listen: false);

    return Consumer<TheData>(
      builder: (context, value, child) => SingleChildScrollView(
        child: Container(
          child: Column(
            children: [
              Container(
                height: 120,
                child: TodaysNote(),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

And this is the TodaysNote class

class TodaysNote extends StatefulWidget {
  TodaysNote({Key key}) : super(key: key);
  bool boxChecked = false;
  @override
  _TodaysNoteState createState() => _TodaysNoteState();
}

class _TodaysNoteState extends State<TodaysNote> {
  var theDataProvider;

  Widget build(BuildContext context) {
    theDataProvider = Provider.of<TheData>(context, listen: false);

    return Consumer<TheData>(
      builder: (context, value, child) => Container(
        child: theDataProvider.showingTheTodaysList(),
      ),
    );
  }
}

But now when I go to another screen and then come to the previous screen. I see the updated list. I have wrapped the main file with provider, wrapped the files with consumer but did not work.
What might be the reason behind this?

1 Answers

try using this:

Navigator.push( context, MaterialPageRoute( builder: (context) => SecondPage()), ).then((value) => setState(() {}));

Related