Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[go_router] Route triggered twice after navigating from 3rd party app by link (deep linking) #137037

Open
supervital opened this issue Oct 22, 2023 · 9 comments
Labels
found in release: 3.13 Found to occur in 3.13 found in release: 3.16 Found to occur in 3.16 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: go_router The go_router package P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. team-go_router Owned by Go Router team triaged-go_router Triaged by Go Router team

Comments

@supervital
Copy link

supervital commented Oct 22, 2023

Hello.

I am currently working on implementing deep linking functionality in my application, which allows users to open the app via links received in emails. However, I have encountered an issue where the go_router behaves unexpectedly. Specifically, the route specified in go_router is triggered twice when the app is launched from a cold start. After the app has started, deep linking works as expected.

Steps to reproduce:

  1. Run the Flutter project on an Android device.
  2. Close the app using the back button on Android to ensure that the app is completely closed.
  3. Open an email or any other app containing a deep link.
  4. Click on the deep link to open the application.

Expected Behavior:
The specified widget should be created only once when the deep link is triggered, ensuring a single instance of the widget.

Actual Behavior:
The widget is created twice when the application is launched from a cold start on Android.

Reproducibility:
The issue consistently occurs when the application is launched from a cold start on Android. It does not occur when the application is already running.

I use go_router 12.0.0.

Example from https://github.com/flutter/codelabs/tree/main/deeplink_cookbook with minor changes:

Android manifest file:

<activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleInstance" // Added this mode to avoid issue with multiple activities

          ...........

            <meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="http" android:host="example.com" />
                <data android:scheme="https" />
</activity>

Dart code:

void main() => runApp(MaterialApp.router(routerConfig: router));

/// This handles '/' and '/details'.
final router = GoRouter(
  debugLogDiagnostics: kDebugMode,
  redirect: (context, state) {
    print('GoRouter redirect ${state.matchedLocation}');
    return null;
  },
  initialLocation: '/test',
  routes: [
    GoRoute(
      path: '/test',
      builder: (_, __) => Scaffold(
        appBar: AppBar(title: const Text('Home Screen')),
      ),
    ),
    GoRoute(
      path: '/details',
      builder: (_, __) {
        print('Open details screen');
        return Scaffold(
          appBar: AppBar(title: const Text('Details Screen')),
        );
      },
    ),
  ],
);

Expected results

The route should be triggered once.

Actual results

The route is triggered twice after the initial click on the link.

Logs

Logs
[GoRouter] setting initial location /test
I/flutter (32552): GoRouter redirect /details
[GoRouter] Using MaterialApp configuration
I/flutter (32552): Open details screen
I/flutter (32552): Open details screen
I/flutter (32552): GoRouter redirect /details
I/flutter (32552): Open details screen
I/flutter (32552): GoRouter redirect /details
I/flutter (32552): Open details screen

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.13.3, on macOS 12.6 21G115 darwin-x64, locale en-UA)
    • Flutter version 3.13.3 on channel stable at /Users/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2524052335 (7 weeks ago), 2023-09-06 14:32:31 -0700
    • Engine revision b8d35810e9
    • Dart version 3.1.1
    • DevTools version 2.25.0

⣟                                                                                                                                                                 [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.12.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.81.1)
    • VS Code at /Users/Downloads/Visual Studio Code.app/Contents
    • Flutter extension version 3.72.0

[✓] Connected device (3 available)
    • sdk gphone x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • macOS (desktop)         • macos         • darwin-x64     • macOS 12.6 21G115 darwin-x64
    • Chrome (web)            • chrome        • web-javascript • Google Chrome 118.0.5993.88

[✓] Network resources
    • All expected network resources are available.

• No issues found!
@supervital supervital closed this as not planned Won't fix, can't repro, duplicate, stale Oct 22, 2023
@supervital supervital reopened this Oct 24, 2023
@supervital supervital changed the title Route triggered twice after navigating from 3rd party app by link (deep linking) [go_router] Route triggered twice after navigating from 3rd party app by link (deep linking) Oct 24, 2023
@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Oct 25, 2023
@darshankawar
Copy link
Member

Thanks for the report @supervital
Can you take a look at this and see if it helps to give expected results for your scenario ?

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 25, 2023
@supervital
Copy link
Author

supervital commented Oct 25, 2023

Thanks for the report @supervital Can you take a look at this and see if it helps to give expected results for your scenario ?

No, it doesn’t give me the expected result.

GoRouter() is still triggered twice, even though I removed the GoRouter('/details') route.
Please see the updated dart code, and my logs with comments:

void main() => runApp(MaterialApp.router(routerConfig: router));

final router = GoRouter(
  debugLogDiagnostics: kDebugMode,
  redirect: (context, state) {
    print('GoRouter.redirect: ${state.matchedLocation}');

    if (state.matchedLocation == '/details') {
      print('GoRouter.redirect: hello deep link');
      return '/my_details';
    }

    return null;
  },
  initialLocation: '/test',
  routes: [
    GoRoute(
      path: '/test',
      builder: (_, __) {
        print('GoRoute.builder: Open home screen');
        return Scaffold(appBar: AppBar(title: const Text('Home Screen')));
      },
    ),
    GoRoute(
      path: '/my_details',
      builder: (_, __) {
        print('GoRoute.builder: Open details screen');
        return Scaffold(
          appBar: AppBar(title: const Text('Details Screen')),
        );
      },
    ),
  ],
);

And logs for three scenarios here. The first one - the code is run from AndroidStudio, and the BackButton pressed:

[GoRouter] setting initial location /test
I/flutter ( 7591): GoRouter.redirect: /test
I/flutter ( 7591): GoRoute.builder: Open home screen
[GoRouter] Using MaterialApp configuration
// **Here, I pressed the back button, and for some reason, a new route was triggered.**
I/flutter ( 7591): GoRoute.builder: Open home screen

The second scenario: the app is closed with the back button, and I am clicking a link (https://example.com/details):

[GoRouter] setting initial location /test
I/flutter ( 7591): GoRouter.redirect: /details
I/flutter ( 7591): GoRouter.redirect: hello deep link
[GoRouter] No initial matches: https://example.com/details
I/flutter ( 7591): GoRouter.redirect: /my_details
[GoRouter] redirecting to RouteMatchList(/my_details)
[GoRouter] Using MaterialApp configuration
I/flutter ( 7591): GoRoute.builder: Open details screen
I/flutter ( 7591): GoRoute.builder: Open details screen

As you can see 'Open details screen' is printed twice.

The third scenario: I closed the app by pressing the back button, and then I clicked a link. After that, I minimized the app using the home button, and I clicked the link again (I performed this step twice):

[GoRouter] setting initial location /test
I/flutter ( 7591): GoRouter.redirect: /details
[GoRouter] No initial matches: https://example.com/details
I/flutter ( 7591): GoRouter.redirect: hello deep link
I/flutter ( 7591): GoRouter.redirect: /my_details
[GoRouter] redirecting to RouteMatchList(/my_details)
[GoRouter] Using MaterialApp configuration
I/flutter ( 7591): GoRoute.builder: Open details screen
I/flutter ( 7591): GoRoute.builder: Open details screen
// T**he app was minimized and the link was clicked**
I/flutter ( 7591): GoRouter.redirect: /details
[GoRouter] No initial matches: /details
I/flutter ( 7591): GoRouter.redirect: hello deep link
I/flutter ( 7591): GoRouter.redirect: /my_details
[GoRouter] redirecting to RouteMatchList(/my_details)
I/flutter ( 7591): GoRoute.builder: Open details screen
// **The app was minimized and the link was clicked**
I/flutter ( 7591): GoRouter.redirect: /details
I/flutter ( 7591): GoRouter.redirect: hello deep link
I/flutter ( 7591): GoRouter.redirect: /my_details
[GoRouter] No initial matches: /details
[GoRouter] redirecting to RouteMatchList(/my_details)
I/flutter ( 7591): GoRoute.builder: Open details screen

As you can see from the logs, the route GoRoute(path: '/my_details') was triggered twice for the very first time, and then it worked as expected.

Additionally, any route (e.g., GoRoute(path: '/test') in my case) was also triggered twice after I closed the app using the back button.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 25, 2023
@darshankawar
Copy link
Member

Thanks for the update. Seeing the same behavior as reported.

stable, master flutter doctor -v
[!] Flutter (Channel stable, 3.13.8, on macOS 12.2.1 21D62 darwin-x64, locale
    en-GB)
    • Flutter version 3.13.8 on channel stable at
      /Users/dhs/documents/fluttersdk/flutter
    ! Warning: `flutter` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
      your current Flutter SDK checkout at
      /Users/dhs/documents/fluttersdk/flutter. Consider adding
      /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
    ! Warning: `dart` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
      current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
      Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
      of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6c4930c4ac (5 days ago), 2023-10-18 10:57:55 -0500
    • Engine revision 767d8c75e8
    • Dart version 3.1.4
    • DevTools version 2.25.0
    • If those were intentional, you can disregard the above warnings; however
      it is recommended to use "git" directly to perform update checks and
      upgrades.

[!] Xcode - develop for iOS and macOS (Xcode 12.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    ! Flutter recommends a minimum Xcode version of 13.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] VS Code (version 1.62.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.21.0

[✓] Connected device (5 available)
    • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
      ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
    • macOS (desktop)            • macos                                    •
      darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
    • Chrome (web)               • chrome                                   •
      web-javascript • Google Chrome 98.0.4758.80

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

[!] Flutter (Channel master, 3.16.0-17.0.pre.40, on macOS 12.2.1 21D62
    darwin-x64, locale en-GB)
    • Flutter version 3.16.0-17.0.pre.40 on channel master at
      /Users/dhs/documents/fluttersdk/flutter
    ! Warning: `flutter` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
      your current Flutter SDK checkout at
      /Users/dhs/documents/fluttersdk/flutter. Consider adding
      /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
    ! Warning: `dart` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
      current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
      Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
      of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2f45754aab (18 minutes ago), 2023-10-26 00:51:43 -0400
    • Engine revision f844ab9333
    • Dart version 3.3.0 (build 3.3.0-56.0.dev)
    • DevTools version 2.28.1
    • If those were intentional, you can disregard the above warnings; however
      it is recommended to use "git" directly to perform update checks and
      upgrades.

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/dhs/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for
      more details.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13C100
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 65.1.4
    • Dart plugin version 213.7228

[✓] VS Code (version 1.62.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.29.0

[✓] Connected device (3 available)
    • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios
      • iOS 15.3.1 19D52
    • macOS (desktop)           • macos                                    •
      darwin-x64     • macOS 12.2.1 21D62 darwin-x64
    • Chrome (web)              • chrome                                   •
      web-javascript • Google Chrome 109.0.5414.119

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
      
[!] Xcode - develop for iOS and macOS (Xcode 12.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    ! Flutter recommends a minimum Xcode version of 13.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] VS Code (version 1.62.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.21.0

[✓] Connected device (5 available)
    • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
      ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
    • macOS (desktop)            • macos                                    •
      darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
    • Chrome (web)               • chrome                                   •
      web-javascript • Google Chrome 98.0.4758.80

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.



@darshankawar darshankawar added package flutter/packages repository. See also p: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on p: go_router The go_router package found in release: 3.13 Found to occur in 3.13 found in release: 3.16 Found to occur in 3.16 team-go_router Owned by Go Router team and removed in triage Presently being triaged by the triage team labels Oct 26, 2023
@supervital
Copy link
Author

supervital commented Oct 26, 2023

I'm also curious about the route triggered when the app is closed using the back button.

Please see the logs:

[GoRouter] setting initial location /test
I/flutter ( 7591): GoRouter.redirect: /test
I/flutter ( 7591): GoRoute.builder: Open home screen // the route is triggered the first time
[GoRouter] Using MaterialApp configuration
// Here, I pressed the back button, and for some reason, a new route was triggered.
I/flutter ( 7591): GoRoute.builder: Open home screen // the route is triggered a second time.

Could you comment on this behavior?

Thanks.

@chunhtai chunhtai added P2 Important issues not at the top of the work list triaged-go_router Triaged by Go Router team labels Oct 26, 2023
@novjean
Copy link

novjean commented Jan 12, 2024

I have been facing the same issue on my app from Go Router versions 6 to 12, has there been a fix for this yet?

@haticeay
Copy link

haticeay commented Feb 8, 2024

I am having the same problem in my application, is there a solution?

@supervital
Copy link
Author

I am having the same problem in my application, is there a solution?

@haticeay Play around redirect in GoRouter.

@haticeay
Copy link

haticeay commented Feb 9, 2024

@supervital the code worked thank you

@mako-yolo
Copy link

mako-yolo commented Jun 10, 2024

This workaround seems to solve the problem.
What do you think could go wrong?

String _lastRoute = "";

// ignore: long-method
GoRouter createRouter({String? initialLocation}) {
  return GoRouter(
  navigatorKey: _rootNavigatorKey,
  initialLocation: NavPaths.root.urlPattern,
  redirect: (context, state) async {
    if (state.matchedLocation == _lastRoute) {
        return '';
    }
  
   _lastRoute = state.redirectParams.matchedLocation;
  ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
found in release: 3.13 Found to occur in 3.13 found in release: 3.16 Found to occur in 3.16 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: go_router The go_router package P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. team-go_router Owned by Go Router team triaged-go_router Triaged by Go Router team
Projects
None yet
Development

No branches or pull requests

6 participants