setState() called after dispose()

后端 未结 6 1930
迷失自我
迷失自我 2020-12-24 04:35

When I click the raised button, the timepicker is showing up. Now if I wait like 5 seconds and then confirm the time this error will occur setState() called after d

6条回答
  •  一整个雨季
    2020-12-24 04:53

    Just check boolean property mounted of the state class of your widget before calling setState().

    if (this.mounted) {
      setState(() {
        // Your state change code goes here
      });
    }
    

    Or even more clean approach Override setState method in your StatelfulWidget class.

    class DateTimeButton extends StatefulWidget {
      @override
      void setState(fn) {
        if(mounted) {
          super.setState(fn);
        }
      }
    }
    

提交回复
热议问题