Search code examples
fluttertimernavigator

how to pop page after condition is true in flutter?


I am trying to build a timer app where screen will pop after time become null the sample code is following

                              Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: CountdownTimer(
                                    endTime: endTime,
                                    widgetBuilder: (_, CurrentRemainingTime time) {
                                      if (time == null) {
                                          Navigator.of(context).pop();

                                      } else {
                                        return Text(
                                            '${(time.hours == null) ? "00" : time.hours}:${(time.min == null) ? "00" : time.min}:${(time.sec == null) ? "00" : time.sec}');
                                      }
                                    }),
                       

error Shown As Follows

E/flutter (21479): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed 
assertion: line 4334 pos 12: '<optimized out>': is not true.
E/flutter (21479): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter (21479): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter (21479): #2      NavigatorState._pushEntry (package:flutter/src/widgets/navigator.dart:4334:12)
E/flutter (21479): #3      NavigatorState.push (package:flutter/src/widgets/navigator.dart:4270:5)
E/flutter (21479): #4      showGeneralDialog (package:flutter/src/widgets/routes.dart:1835:66)
E/flutter (21479): #5      Alert.show (package:rflutter_alert/src/alert.dart:57:18)

Solution

  • You can copy paste run full code below
    You can use addPostFrameCallback and return Container()
    code snippet

    widgetBuilder: (_, CurrentRemainingTime time) {
          if (time == null) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              Navigator.of(context).pop();
            });
            return Container();
    

    working demo

    enter image description here

    full code

    import 'package:flutter/material.dart';
    import 'package:flutter_countdown_timer/flutter_countdown_timer.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text("test"),
            ),
            body: Center(
              child: RaisedButton(
                child: Text('To CountDown page'),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => CountDownPage()),
                  );
                },
              ),
            ));
      }
    }
    
    class CountDownPage extends StatefulWidget {
      CountDownPage({Key key}) : super(key: key);
    
      @override
      _CountDownPageState createState() => _CountDownPageState();
    }
    
    class _CountDownPageState extends State<CountDownPage> {
      int endTime = DateTime.now().millisecondsSinceEpoch + 1000 * 3;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("count down"),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: CountdownTimer(
                        endTime: endTime,
                        widgetBuilder: (_, CurrentRemainingTime time) {
                          if (time == null) {
                            WidgetsBinding.instance.addPostFrameCallback((_) {
                              Navigator.of(context).pop();
                            });
                            return Container();
                          } else {
                            return Text(
                                '${(time.hours == null) ? "00" : time.hours}:${(time.min == null) ? "00" : time.min}:${(time.sec == null) ? "00" : time.sec}');
                          }
                        })),
              ],
            ),
          ),
        );
      }
    }