Home > Enterprise >  How to get data from Native Android Activity to Flutter route library?
How to get data from Native Android Activity to Flutter route library?

Time:08-09

Flutter allows you to create a library that can be integrated into an Android Native project. As it is shown in the official documentation. However it does not contain any information about sharing data between the two modules.

I was able to find in another page this piece of code witch is supposedly able to share data from the current android activity to the FlutterActivity. But it does not show you how to retrieve that data from the flutter module.

viewModel.onVClick = { it ->
    Log.d("FlutterLaunchWithID", it)
    requireActivity().startActivity(
        FlutterActivity
            .withNewEngine()
            .dartEntrypointArgs(listOf(it))
            .initialRoute("/")
            .build(requireContext())
    )
}

How would I go to get that data? Here is the Flutter code, but I don't think it'll help since it's the common Flutter Demo:

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

CodePudding user response:

Found the solution. You just have to add the arguments to the main method

void main(List<String> arguments) {
  for (final arg in arguments) {
    print('Android app argument: $arg');
  }
  runApp(const MyApp());
}
  • Related