@@ -79,20 +79,22 @@ class _MyHomePageState extends State<MyHomePage> {
7979 onPressed: () {
8080 _battery.batteryLevel.then (
8181 (batteryLevel) {
82- showDialog <void >(
83- context: context,
84- builder: (_) => AlertDialog (
85- content: Text ('Battery: $batteryLevel %' ),
86- actions: < Widget > [
87- TextButton (
88- onPressed: () {
89- Navigator .pop (context);
90- },
91- child: const Text ('OK' ),
92- )
93- ],
94- ),
95- );
82+ if (context.mounted) {
83+ showDialog <void >(
84+ context: context,
85+ builder: (_) => AlertDialog (
86+ content: Text ('Battery: $batteryLevel %' ),
87+ actions: < Widget > [
88+ TextButton (
89+ onPressed: () {
90+ Navigator .pop (context);
91+ },
92+ child: const Text ('OK' ),
93+ )
94+ ],
95+ ),
96+ );
97+ }
9698 },
9799 );
98100 },
@@ -103,27 +105,29 @@ class _MyHomePageState extends State<MyHomePage> {
103105 onPressed: () {
104106 _battery.isInBatterySaveMode.then (
105107 (isInPowerSaveMode) {
106- showDialog <void >(
107- context: context,
108- builder: (_) => AlertDialog (
109- title: const Text (
110- 'Is in Battery Save mode?' ,
111- style: TextStyle (fontSize: 20 ),
112- ),
113- content: Text (
114- "$isInPowerSaveMode " ,
115- style: const TextStyle (fontSize: 18 ),
108+ if (context.mounted) {
109+ showDialog <void >(
110+ context: context,
111+ builder: (_) => AlertDialog (
112+ title: const Text (
113+ 'Is in Battery Save mode?' ,
114+ style: TextStyle (fontSize: 20 ),
115+ ),
116+ content: Text (
117+ "$isInPowerSaveMode " ,
118+ style: const TextStyle (fontSize: 18 ),
119+ ),
120+ actions: < Widget > [
121+ TextButton (
122+ onPressed: () {
123+ Navigator .pop (context);
124+ },
125+ child: const Text ('Close' ),
126+ )
127+ ],
116128 ),
117- actions: < Widget > [
118- TextButton (
119- onPressed: () {
120- Navigator .pop (context);
121- },
122- child: const Text ('Close' ),
123- )
124- ],
125- ),
126- );
129+ );
130+ }
127131 },
128132 );
129133 },
0 commit comments