popAndPushNamed - Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct)

370

Use mounted getter to determine if the State is still active

    Future.delayed(const Duration(milliseconds: 730), () {
     if(mounted)
      Navigator.of(context).popAndPushNamed(
        PageNames.something,
        arguments: {
          "info": null,
        },
      );
    });
Share:
370
Daniel Roldán
Author by

Daniel Roldán

Updated on January 03, 2023

Comments

  • Daniel Roldán
    Daniel Roldán over 1 year

    I briefly explain my problem, when I create and name "something" in my application, I let 760 milliseconds pass to go to another page. However the problem is that in that lapse of time, if I exit with the back button of my AppBar, I get the following error (the function that inidicates onTitleSelectionAtCreate is the one that I use the popAndPushNamed)

    [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
    E/flutter (28951): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
    

    enter image description here

    Here is my code :

    class InitialRemarkInfo extends StatefulWidget {
      final bool? isCreated;
    
      const InitialRemarkInfo({
        Key? key,
        required this.isCreated,
      }) : super(key: key);
    
      @override
      _InitialRemarkInfoState createState() => _InitialRemarkInfoState();
    }
    
    class _InitialRemarkInfoState extends State<InitialRemarkInfo>
        with TickerProviderStateMixin {
    
      double _height = 0;
      double _width = 0;
      bool _resized = false;
    
      @override
      void initState() {
        super.initState();
    
        if (!this.widget.isCreated!)
          BlocProvider.of<SomethingBloc>(context).add(InitializeEvent());
      }
    
      @override
      Widget build(BuildContext context) {
        return WillPopScope(
          onWillPop: this._onPopBack,
          child: Scaffold(
            // * APP BAR
            appBar: AppBar(
              title: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Text(this.localizedString('newSomething')!),
                ],
              ),
              leading: IconButton(
                splashRadius: 1,
                icon: Icon(Icons.arrow_back),
                onPressed: _onPopBack,
              ),
              backgroundColor: DesignConstants.darkBlueBackground,
            ),
    
            body: BlocConsumer<SomethingBloc, SomethingState>(
              // * LISTENER
              listener: (context, state) async {
    
                [...]
    
              },
              builder: (context, state) {
                // * LOADED
                if (state is SomethingLoaded)
                  return Stack(
                    children: [
                      SingleChildScrollView(
                        physics: AlwaysScrollableScrollPhysics(),
                        child: Column(
                          children: [
                            // * GROWING SPACE (to simulate animation)
                            new AnimatedSize(
                              curve: Curves.easeIn,
                              child: new Container(
                                width: _width,
                                height: _height,
                                color: Colors.transparent,
                              ),
                              duration: new Duration(milliseconds: 700),
                            ),
    
                            [...]
    
                            // * TITLE
                            DataRo(
                              title: 'title',
                              icon: Icons.arrow_drop_down,
                              isCreation: true,
                              onTitleSelectionAtCreate: () =>
                                  _onTitleSelectionAtCreate(), // Function
                            ),
                          ],
                        ),
                      ),
                    ],
                  );
    
                // * DEFAULT
                return Container();
              },
            ),
          ),
        );
      }
    
      //============================================================================
      // ON POP BACK
      //
      Future<bool> _onPopBack() async {
        BlocProvider.of<SomethingBloc>(context).add(
            ReloadList(isCreation: true, isSaving: false));
    
        return false;
      }
    
    
      // ==========================================================================
      // ON TITLE SELECTION AT CREATE
      //
      void _onTitleSelectionAtCreate() {
          setState(() {
            if (_resized) {
              this._resized = false;
              this._height = 0;
              this._width = 0;
            } else {
              this._resized = true;
              this._height = Device.screenHeight / 3;
              this._width = Device.screenWidth;
            }
          }); // To animate something not relevant
        
        Future.delayed(const Duration(milliseconds: 730), () {
          Navigator.of(context).popAndPushNamed(
            PageNames.something,
            arguments: {
              "info": null,
            },
          );
        });
      }
    }