Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dart Unhandled Exception: setState() called after dispose() in flutter

I have a page on which I display data via BloC. I added the isLoading variable to display the loading indicator, which is true after two seconds, this value changes to false and the loading indicator disappears and the data is displayed. But I got an error because I use setState(). Tell me how to fix this error?

Widget _child(Size size, BuildContext context, double topPadding) {
    return BlocBuilder<MycarsCubit, MycarsState>(
      builder: (context, stateElectricvehicles) {
        final ElectricvehiclesCubit cubit =
            BlocProvider.of<ElectricvehiclesCubit>(context);

        if (stateElectricvehicles is MycarsInitial) {
          carNumber.text = stateElectricvehicles.number;
          carNumber.selection = TextSelection.fromPosition(
            TextPosition(offset: carNumber.text.length),
          );

          if (stateElectricvehicles.id == 0) {
            savedCarId = stateElectricvehicles.id;
          }

          Future.delayed(const Duration(seconds: 2), () {
            setState(() {
              isLoading = false;
            });
          });

          final SharedPrefs prefs = SharedPrefs();
          return FutureBuilder<int?>(

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Always add a check mounted (available in StatefulWidget) before calling setState in async function.

          Future.delayed(const Duration(seconds: 2), () {
           if(mounted) {
            setState(() {
              isLoading = false;
            });
           }
          });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading