简体   繁体   中英

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

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'),
    );
  }
}

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM