Robson Cardozo
Robson Cardozo

Reputation: 33

How to call intent method in Flutter?

I need to integrate my app with another app in the company. I have to send some parameters and get the response in my app. I was seeing a lot of plugins is the pub.dev but none of them worked.

This is how i did in Java/Android

                Intent myapp = new Intent("android.intent.action.GET_PIN_CODE");

                myapp.putExtra("sharedDocument",USER_ID);
                myapp.putExtra("sharedText",DISPLAY_MSG);
                myapp.putExtra("sharedNonce",nonce);
                myapp.putExtra("sharedHmac",hmac);

                startActivityForResult(myapp,REQUEST_OK);

Now i have to do this in Flutter, please help me!! :D

I already did this, but didn't worked.

CODE IN DART:

  Future<List<String>> startActivityForResult(String action, String id, String text, String nonce, String hmac) {
    const MethodChannel _channel = const MethodChannel('intent');
    Map<String, dynamic> parameters = {};
    Map<String, dynamic> _extra;

    _extra["sharedDocument"] = id;
    _extra["sharedText"] = text;
    _extra["sharedNonce"] = nonce;
    _extra["sharedHmac"] = hmac;

    parameters['action'] = action;
    parameters['extra'] = _extra;

    return _channel
        .invokeMethod('startActivityForResult', parameters)
        .then((data) => List<String>.from(data));
  }

I called the method:

startActivityForResult('android.intent.action.GET_PIN_CODE', USER_ID, DISPLAY_MSG, nonce, hmac.toString()).then((data) => print(data));

But de output was this:

The method '[]=' was called on null.
Receiver: null
Tried calling: []=("sharedDocument", "001023030")

Upvotes: 2

Views: 5107

Answers (1)

Yakub Pasha
Yakub Pasha

Reputation: 533

You should be implementing the method channel inside your MainActivity under the android folder of your flutter project.

    class MainActivity: FlutterActivity() {
      private val CHANNEL = "intent" // your channel name

      override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
          call, result ->
          if (call.method == "startActivityForResult") { 
           // your method name used for invoking this from flutter
             //Do your intent calling here
          }else{
             result.notImplemented()
          }
        }
      }
    }

Invoke the native method from you your Flutter Widget like this

Future<void> _startActivityForResult() async {

  const platform = const MethodChannel('intent');
  try {
    final int result = await platform.invokeMethod('startActivityForResult');
    print(result);
  } on PlatformException catch (e) {
    print('${e.message}');
  }
}

source:https://flutter.dev/docs/development/platform-integration/platform-channels and you can check the example here: https://www.nplix.com/2019/09/flutter-how-to-start-android-activity.html

Upvotes: 2

Related Questions