简体   繁体   English

如何从 Native Android Activity 获取数据到 Flutter 路由库?

[英]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. Flutter 允许您创建一个可以集成到 Android Native 项目中的库。 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.我能够在另一个页面中找到这段代码,据说它能够将当前 android 活动的数据共享到 FlutterActivity。 But it does not show you how to retrieve that data from the flutter module.但它没有向您展示如何从 flutter 模块中检索该数据。

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?我将如何 go 获取该数据? Here is the Flutter code, but I don't think it'll help since it's the common Flutter Demo:这是 Flutter 代码,但我认为它没有帮助,因为它是常见的 Flutter 演示:

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您只需将 arguments 添加到 main 方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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