marcglasberg / back_button_interceptor

Flutter Package: May be used to intercept the Android back-button, as an alternative to `WillPopScope`.
BSD 2-Clause "Simplified" License
105 stars 21 forks source link

ifRouteChanged() context parameter problem #8

Closed NassB closed 3 years ago

NassB commented 3 years ago

I am trying to implement your lib but I am facing a problem when I am trying to check back button behavior when I push a modal (CustomDialog).

I have two BackButtonInterceptor implemented, one in my main.dart and one in another class :

BackButtonInterceptor: Custom, z-index: null (ifNotYetIntercepted: false). BackButtonInterceptor: GlobalBackButtonInterceptor, z-index: null (ifNotYetIntercepted: false).

I/flutter (19197): The BackButtonInterceptor threw an ERROR: Assertion failed: "The ifRouteChanged() method can only be called if the context parameter was passed to the BackButtonInterceptor.add() method.".
E/flutter (19197): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Assertion failed: "The ifRouteChanged() method can only be called if the context parameter was passed to the BackButtonInterceptor.add() method."
E/flutter (19197): #0      BackButtonInterceptor.errorProcessing.<anonymous closure>.<anonymous closure> (package:back_button_interceptor/src/back_button_interceptor.dart:22:38)
E/flutter (19197): #1      new Future.delayed.<anonymous closure> (dart:async/future.dart:326:39)
E/flutter (19197): #2      _rootRun (dart:async/zone.dart:1182:47)
E/flutter (19197): #3      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (19197): #4      _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (19197): #5      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (19197): #6      _rootRun (dart:async/zone.dart:1190:13)
E/flutter (19197): #7      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (19197): #8      _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter (19197): #9      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (19197): #10     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter (19197): #11     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter (19197): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

I tried your example main_complex_example and it's working.

That's my code :

class CustomPage extends StatefulWidget {
  @override
  _CustomPageState createState() => _CustomPageState();
}

class _CustomPageState extends State<CustomPage> {
  @override
  void initState() {
    super.initState();
    BackButtonInterceptor.add(myInterceptor, name: "Custom", context: context);
  }

  bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
    print(BackButtonInterceptor.describe());
    print(stopDefaultButtonEvent);
    if (stopDefaultButtonEvent) return false;
    if (info.ifRouteChanged(context)) return false;
    return true;
  }

  @override
  void dispose() {
    BackButtonInterceptor.removeByName("Custom");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text("Custom"),
        ),
        body: Container(
            child: IconButton(
          icon: Icon(
            Icons.share,
            color: Colors.blueGrey[500],
            size: 30,
          ),
          onPressed: () => Navigator.of(context).push(PageRouteBuilder(
              barrierDismissible: true, opaque: false, 
              pageBuilder: (context, _, __) => CustomDialog(...)),
        )));
  }
}

Flutter doctor -v :

[✓] Flutter (Channel stable, 1.22.0, on Mac OS X 10.15.6 19G2021, locale fr-FR)
    • Flutter version 1.22.0 at /Users/******/Workspace/flutter
    • Framework revision d408d302e2 (7 days ago), 2020-09-29 11:49:17 -0700
    • Engine revision 5babba6c4d
    • Dart version 2.10.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/*******/Library/Android/sdk
    • Platform android-30, build-tools 30.0.1
    • ANDROID_HOME = /Users/*****/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/*****/Library/Android/sdk
    • Java binary at: /Users/******/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6626763/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.0.1, Build version 12A7300
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Users/*******/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6626763/Android
      Studio.app/Contents
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (1 available)            
    • MI PAD 4 (mobile) • 5a95aa78 • android-arm64 • Android 9 (API 28)

• No issues found!
marcglasberg commented 3 years ago

Problem was your routes are not named, and I've implemented this for named routes only. I am fixing this.

marcglasberg commented 3 years ago

Please try version [4.4.0]

Please, let me know in case the problem persists. Thanks.