Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameters to flutter module from android or iOS

I'm experimenting with integrating a flutter module in an existing android app. Starting a view works fine using this code:

private fun startFlutterActivity() {
    val intent = FlutterActivity.withCachedEngine("default_engine").build(this)
    startActivity(intent)
}

But I'm not sure on how to pass additional data e.g. an id to fetch data for to the flutter component. I know you can establish a message channel but this seems like a lot of overhead for passing stuff around.

like image 878
SebRut Avatar asked Jan 21 '20 14:01

SebRut


People also ask

How do you integrate a flutter module into an Android project?

Using the File > New > New Module… menu in Android Studio in your existing Android project, you can either create a new Flutter module to integrate, or select an existing Flutter module that was created previously. If you create a new module, you can use a wizard to select the module name, location, and so on.


1 Answers

The easiest way is to pass the id through to the flutter engine via the initial route.

So like:

    flutterEngine = new FlutterEngine(this);
    // Configure an initial route.
    flutterEngine.getNavigationChannel().setInitialRoute("showStuf?id=$id");

Or, in your example, you can simply add the intent extra FlutterActivityLaunchConfigs.EXTRA_INITIAL_ROUTE to the intent.

On the flutter side you would then simply parse the initial route in your onGenerateRoute handler for your MaterialApp or other navigator config.

like image 54
Herbert Poul Avatar answered Oct 04 '22 14:10

Herbert Poul