1

I'm working on a windows application with flutter desktop, for now, everything seems to work smoothly and with no problems. But I suspect that I caused a memory leak when using PageView !!

Basically, I have PageViews with 6 pages. One of those pages is a form with a lot of TextEditingController and yea I'm calling dispose() for each TextEditingController and also for all BLoCs I'm using.

Now when The user submits the data and I get a reply from the API that the data was stored successfully I show a success dialog:

return showDialog<void>(
  context: context,
  barrierDismissible: false,
  builder: (BuildContext context) {
    return AlertDialog(
      title: Text('Succès'),
      content: Text(message),
      actions: [
        FlatButton(
          child: Text('Sortie'),
          textColor: Colors.white,
          color: kColorGreen,
          onPressed: () {
            Navigator.of(context).pop();
            pageController.jumpToPage(2);
            // Tried calling dispose here !!
            // dispose();
          },
        ),
      ],
    );
  },
);

OnPressed: I take the user to another page. And it works BUT when I navigate back to the form page all the form fields are still filled with the old data and the dispose method never gets called !!

I tried calling the dispose method by myself before navigating to another page but no luck !!

So my question is: How can I dispose of all the TextEditingController and the BLoCs present on the form page when navigating out of it?

Abdelbaki Boukerche
  • 1,602
  • 1
  • 8
  • 20
  • I had a similar issue with PageView, with each page containing a ListView with different types of inputs in the lists. What I did was log every init and dispose method of the widgets on each of the pages. Eventually figured out that these widgets initialise and dispose unpredictably, especially if you invoke a dialog and then dismiss it with the Navigator. Our system uses MobX, and we populate the fields in each of the inputs with data from our MobX stores. Best approach is to populate every widget with state from your store/BLoC on initialisation, and force updates with setState if necessary. – Dane_duPlessis Jan 13 '21 at 03:33

0 Answers0