Home > Blockchain >  Error when "Navigator.pop(context);" execute
Error when "Navigator.pop(context);" execute

Time:09-25

I builded a flutter app that navigating between two pages and it's working but after a while on pressing the image that run the pop code I am getting this error:


════════ Exception caught by gesture ═══════════════════════════════════════════
The following RangeError was thrown while handling a gesture:
RangeError (index): Invalid value: Only valid value is 0: -1

When the exception was thrown, this was the stack
#0      List.[] (dart:core-patch/growable_array.dart:260:36)
#1      NavigatorState.didStartUserGesture
package:flutter/…/widgets/navigator.dart:4982
#2      new _CupertinoBackGestureController
package:flutter/…/cupertino/route.dart:714
#3      CupertinoRouteTransitionMixin._startPopGesture
package:flutter/…/cupertino/route.dart:231
#4      CupertinoRouteTransitionMixin.buildPageTransitions.<anonymous closure>
package:flutter/…/cupertino/route.dart:278
#5      _CupertinoBackGestureDetectorState._handleDragStart
package:flutter/…/cupertino/route.dart:627
#6      DragGestureRecognizer._checkStart.<anonymous closure>
package:flutter/…/gestures/monodrag.dart:429
#7      GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:198
#8      DragGestureRecognizer._checkStart
package:flutter/…/gestures/monodrag.dart:429
#9      DragGestureRecognizer.acceptGesture
package:flutter/…/gestures/monodrag.dart:349
#10     GestureArenaManager._resolveByDefault
package:flutter/…/gestures/arena.dart:251
#11     GestureArenaManager._tryToResolveArena.<anonymous closure>
package:flutter/…/gestures/arena.dart:232
(elided 10 frames from dart:async)
Handler: "onStart"
Recognizer: HorizontalDragGestureRecognizer#4d4ab
    debugOwner: _CupertinoBackGestureDetectorState<dynamic>#dadf1
    start behavior: start
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
'package:flutter/src/cupertino/route.dart': Failed assertion: line 632 pos 12: '_backGestureController != null': is not true.
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
'package:flutter/src/cupertino/route.dart': Failed assertion: line 638 pos 12: '_backGestureController != null': is not true.
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
Bad state: No element
════════════════════════════════════════════════════════════════════════════════

and this is my code that navigate from page one to page two

Center(
                    child: Padding(
                      padding: EdgeInsets.only(top: size.width * .045),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            "Don’t have an account?",
                            style: TextStyle(
                              color: headText,
                              fontSize: size.width * .034,
                              fontWeight: FontWeight.w300,
                            ),
                          ),
                          SizedBox(width: size.width * .010),
                          GestureDetector(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (context) =>
                                      const CreateAccountPage(),
                                ),
                              );
                            },
                            child: Text(
                              "Create one",
                              style: TextStyle(
                                color: Color(0xFF0B8AFF),
                                fontSize: size.width * .034,
                                fontWeight: FontWeight.w300,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),

and this is the code that navigate back to page one

GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Padding(
                          padding: EdgeInsets.only(
                            top: size.width * .010,
                            left: size.width * .025,
                          ),
                          child: SvgPicture.asset(
                            "assets/arrow-left-s-line.svg",
                          ),
                        ),
                      ),

I don't know if it's necessarily happening because of the pop code but it's happening when I tapped there, if someone knows what the problem and how can I fix it, it will be very helpful. Thanks

CodePudding user response:

Just try to remove const keyword for your second route try below code its helpful to you. Refer official documentation here for push and pop routing.

      onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) =>
                                  CreateAccountPage(),
                            ),
                          );
                        },
  • Related