如何在Flutter中从Android java Activity调用Dart方法?

8
我需要从本地Android Java活动中调用在某个 .dart 文件中编写的 Dart 方法,在Flutter中,我该如何做到这一点?

你不能直接调用它,但是你可以使用EventChannel将事件发送到Dart代码中,然后Dart可以执行该方法。 - danypata
可能是重复问题,参考链接:https://dev59.com/aVUL5IYBdhLWcg3wi4iT。 - Harshvardhan Joshi
以上链接都有相同的解决方案,但对于我的情况并不适用:在 Android 端从 Flutter 端调用 Dart 方法。我已经测试过它们了。 - Mohsen Emami
@Mohsen Emami,你有解决方案吗? - Alexufo
2个回答

6
在Flutter中

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ScreenPage(),
    );
  }
}
class ScreenPage extends StatefulWidget {
  @override
  _ScreenPageState createState() => _ScreenPageState();
}

class _ScreenPageState extends State<ScreenPage> {
  
  static const platform = const MethodChannel("myChannel");

  @override
  void initState() {
    platform.setMethodCallHandler(nativeMethodCallHandler);
    super.initState();
  }

  Future<dynamic> nativeMethodCallHandler(MethodCall methodCall) async {
    print('Native call!');
    switch (methodCall.method) {
      case "methodNameItz" :
        return "This data from flutter.....";
        break;
      default:
        return "Nothing";
        break;
    }
  }



  
  
  
  @override
  Widget build(BuildContext context) {

    //return ();
  }
 
}

在Java中


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodChannel;
//import io.flutter.view.FlutterNativeView;


public class MyJavaFile extends FlutterActivity {

    Button clickMeButton;
    MethodChannel channel;

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

        channel = new MethodChannel(getFlutterView(), "myChannel");

        setContentView(R.layout.home_activity);
        clickMeButton = findViewById(R.id.clickMeButton);
        
        clickMeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            
                channel.invokeMethod("methodNameItz", null, new MethodChannel.Result() {
                    @Override
                    public void success(Object o) {
                        Log.d("Results", o.toString());
                    }
                    @Override
                    public void error(String s, String s1, Object o) {
                    }
                    @Override
                    public void notImplemented() {
                    }

                });

            }
        });



    }


}


0
  1. 在Dart代码中添加渠道(与您的支付活动渠道相同)并创建sethandler方法。 static const platform = const MethodChannel('flutter.native/helper'); platform.setMethodCallHandler(nativeMethodCallHandler);
  2. 在Dart代码中创建一个处理方法: Future nativeMethodCallHandler(MethodCall methodCall) async { switch (methodCall.method) { case "UpdatePayment" : ........
  3. 在您的Activity/Java代码中调用此方法: methodChannel.invokeMethod("UpdatePayment",null)

如果你有参数,就将其传递给null的位置 例如:Map<String, Object> arguments = new HashMap<>(); arguments.put("paymentResponse", s); methodChannel.invokeMethod("UpdatePayment",arguments);`
在Dart代码中,你可以这样获取:paymentResponse = methodCall.arguments["paymentResponse"];
- undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接