Flutter Deep Linking

1,762

Solution 1

You need platform specific code to handle deep linking. If you follow link mention in documention, you will find complete example.

private val CHANNEL = "poc.deeplink.flutter.dev/channel"
private var startString: String? = null
override fun configureFlutterEngine(@NonNull flutterEngine:FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)

MethodChannel(flutterEngine.dartExecutor, CHANNEL).setMethodCallHandler { call, result ->
    if (call.method == "initialLink") {
        if (startString != null) {
            result.success(startString)
        }
    }
 }
}


override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

   val intent = getIntent()
   startString = intent.data?.toString()
}

Flutter Code:

class DeepLinkBloc extends Bloc {

 //Event Channel creation
static const stream = const 
EventChannel('poc.deeplink.flutter.dev/events');

//Method channel creation
static const platform = const 
MethodChannel('poc.deeplink.flutter.dev/channel');

 StreamController<String> _stateController = StreamController();

 Stream<String> get state => _stateController.stream;

 Sink<String> get stateSink => _stateController.sink;


//Adding the listener into contructor
DeepLinkBloc() {
  //Checking application start by deep link
  startUri().then(_onRedirected);
  //Checking broadcast stream, if deep link was clicked in opened appication

  stream.receiveBroadcastStream().listen((d) => _onRedirected(d));
}


_onRedirected(String uri) {
  // Here can be any uri analysis, checking tokens etc, if it’s necessary
  // Throw deep link URI into the BloC's stream
  stateSink.add(uri);
}


  @override
  void dispose() {
    _stateController.close();
  }


  Future<String> startUri() async {
    try {
      return platform.invokeMethod('initialLink');
    } on PlatformException catch (e) {
      return "Failed to Invoke: '${e.message}'.";
    }
  }
}

Follow this link for more detail.

https://medium.com/flutter-community/deep-links-and-flutter-applications-how-to-handle-them-properly-8c9865af9283

Solution 2

The Flutter way to do that, assuming you've already made the steps in the guide you posted, is to create a onGenerateRoute and/or onGenerateInitialRoutes handlers in your MaterialApp so that these handlers deals with the routes passed or pushed by the framework according to the described behaviors. You can even create an expected named route coming from a deeplink on the routes property of MaterialApp, even though I believe the dynamic generation of routes is more appropriate due to the dynamic nature of deeplinking, specially if you're dealing with "authentication needed content" inside your app.

Share:
1,762
Bugs Happen
Author by

Bugs Happen

Android Developer. Newbie in Flutter. Nihilist. Gamer.

Updated on December 30, 2022

Comments

  • Bugs Happen
    Bugs Happen over 1 year

    According to the Flutter's official deep linking page, we do not require any plugin or native Android/iOS code for handling deep links.

    But it doesn't really tell us how we can get the data from that link. I'm talking from coding perspective. Sure, they have written in there that:

    enter image description here

    But this does not tell me where should I write what code to actually get the complete link. I've looked for examples/tutorials but I'm unable to find anything that is not using a plugin for handling deep linking.

    Right now, all I've done is add <intent-filter> tags in AndroidManifest.xml file and on clicking the link, my app has started to show up. But I don't know how to extract data from that link.

    Is there anyone who can guide me here? Thanks in advance.

  • Bugs Happen
    Bugs Happen almost 3 years
    If you read this flutter.dev/docs/development/ui/navigation/… you will find out that plugin based method is different than the one I'm questioning about. I don't want to use plugins, I want to use the default deep link handling, the one provided by Flutter.