【问题标题】:how do I pass data from native app to flutter?如何将数据从本机应用程序传递到颤振?
【发布时间】:2019-11-25 22:30:46
【问题描述】:

我已阅读此https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps

我的问题是将数据从现有的原生 android 应用程序传递到颤振模块(例如:令牌、用户名......等)。所以,我想问一下有没有办法在现有的原生应用程序的Native代码和flutter模块的代码之间传递数据?

比如有两个页面,A和B,A是用Java代码写的,B是嵌入flutter view的,我没有找到任何办法将A中的数据传给B中的flutter view。

public class TwoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.two_activity);
        
        //this params passed from HomeActivity 
        String params = getIntent().getStringExtra("params");

        FrameLayout rootView = findViewById(R.id.container);

        View flutterView = Flutter.createView(this, getLifecycle(), "service");

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        rootView.addView(flutterView, layoutParams);
    }
}

这是main.dart

void main() => runApp(chooseWidget(window.defaultRouteName));

Widget chooseWidget(String route) {
  switch(route) {
    case 'service':
      return MyFlutterView();
  }
}

class MyFlutterView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {

  static const platform = const MethodChannel('samples.flutter.dev/start');

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[

          Text(
            'this is a flutter page',
            style: TextStyle(
              fontSize: 14,
              color: Colors.blue
            ),
          ),

          FlatButton(
            onPressed: () {
              platform.invokeMethod('startActivity');
            },
            child: Text('go native page'),
            color: Colors.purple,
            highlightColor: Colors.deepPurple,
          )
        ],
      ),
    );
  }
}

-------------->2019.7.18编辑

Thank you for your help. I found the answer.
1、BasicMessageChannel:use this to pass string or other object.

2、MethodChannel:use this to method invocation

3、EventChannel: use this to event streams

【问题讨论】:

标签: android flutter


【解决方案1】:

您可以为此使用方法通道

private static final String CHANNEL = "AndySample/test";

FlutterEngine flutterEngine=new FlutterEngine(this);
flutterEngine.getDartExecutor().executeDartEntrypoint(
    DartExecutor.DartEntrypoint.createDefault()
);
FlutterEngineCache
    .getInstance()
    .put("my_engine_id", flutterEngine);
flutterEngine.getNavigationChannel().setInitialRoute("/");

    
    MethodChannel mc=new 
    MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(),CHANNEL);
    mc.setMethodCallHandler((methodCall, result) ->
        {
            if(methodCall.method.equals("test"))
            {
                result.success("Hai from android and this is the data yopu sent me "+ methodCall.argument("data"));
//Accessing data sent from flutter
            }
            else
            {
                Log.i("new method came",methodCall.method);
            }

        }
    );

使用此博客了解更多信息

https://medium.com/@andymobitec/flutter-sending-data-between-android-and-flutter-1e0693fbae64

【讨论】:

  • 欢迎来到 Stack Overflow。您是撰写您要链接到的 Medium 文章的那个 Andy 吗?如果是这样,请确保至少披露您的隶属关系,以确保您不会与Stack Overflow's self-promotion and spam rules 发生冲突。
猜你喜欢
  • 1970-01-01
  • 2021-08-08
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
相关资源
最近更新 更多