Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Deep Linking

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.

like image 300
Bugs Happen Avatar asked Nov 06 '22 01:11

Bugs Happen


2 Answers

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

like image 83
bilal Avatar answered Nov 15 '22 07:11

bilal


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.

like image 43
Marcelo Ludovico Avatar answered Nov 15 '22 06:11

Marcelo Ludovico