8

I need to call a dart method written in some .dart file in flutter from a native android java activity, how can I do that?

Mohsen Emami
  • 2,709
  • 3
  • 33
  • 40
  • You can't call it directly, but you could use ```EventChannel``` to send events to the dart code and in return the dart can execute the method. – danypata Dec 03 '19 at 11:54
  • Possible duplicate of [https://stackoverflow.com/questions/50187680/flutter-how-to-call-methods-in-dart-portion-of-the-app-from-the-native-platfor](https://stackoverflow.com/questions/50187680/flutter-how-to-call-methods-in-dart-portion-of-the-app-from-the-native-platfor) – Harshvardhan Joshi Dec 03 '19 at 12:12
  • Does this answer your question? [Flutter: How to call methods in Dart portion of the app, from the native platform using MethodChannel?](https://stackoverflow.com/questions/50187680/flutter-how-to-call-methods-in-dart-portion-of-the-app-from-the-native-platfor) – Sumeet.Jain Dec 03 '19 at 12:24
  • Above links have same solutions that don't work for my case: calling a dart method in flutter end from android end. I tested them. – Mohsen Emami Dec 07 '19 at 08:54
  • @Mohsen Emami Do you have a solution? – Alexufo Aug 27 '20 at 03:15

1 Answers1

6

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

In 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() {
                    }

                });

            }
        });



    }


}

Techalgoware
  • 540
  • 6
  • 11