flutterdartflutter-navigationflutter-deep-link

How to handle deep links with Navigation 1.0 in a Flutter?


I am currently using Navigation 1.0 and want to add deep links within the current navigation setup. I am aware that it is simple to add deep links with Navigation 2.0 using packages like go_router. However, I want to implement deep links without relying on any additional packages such as go_router, uni_links, or Firebase.

I have read a couple of articles where they explain the process using the mentioned packages, I prefer not to use them. i'm expecting to handle deep-links the current navigation 1.0 setup.


Solution

  • Actually it's possible without writing a native code. Here is an example:

    First configure the project for deep linking. Add schemas in the manifest file

    // AndroidManifest.xml
    
    <intent-filter android:autoVerify="false">
        <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="test.com" />
        <data android:scheme="https" />
        <data
            android:host="test.com"
            android:scheme="myapp" />
    </intent-filter>
    

    Then add onGenerateRoute parameter to the MaterialApp widget

    // main.dart
    
    onGenerateRoute: (settings) {
      switch (settings.name) {
        case '/':
          return MaterialPageRoute(builder: (_) => HomePage());
        case '/login':
          return MaterialPageRoute(builder: (_) => LoginPage());
        case '/deep':
          return MaterialPageRoute(builder: (_) => DeepPage());
        default:
          return MaterialPageRoute(builder: (_) => HomePage());
      }
    },
    

    Now, by opening this link the app is opened with the route /deep (DeepPage): myapp://test.com/deep

    Note: This example contains configuration for basic testing of a deep link. For the correct configuration for your project, and using universal app links on both android and iOS, follow instructions on this page (except the GoRouter part).

    And here is the example project