3

I followed this tutorial to integrate a Flutter module to an existing native Android app (host) which worked perfectly fine. I then tried to pass values from the host app to the Flutter module and followed this.

Host app

startActivity(
    FlutterActivity.
    createDefaultIntent(getApplicationContext()).
    putExtra("myKey", "From Native Intent")
);

main.dart:

MaterialApp(
      debugShowCheckedModeBanner: false,
      title: Strings.appName,
      theme: ThemeManager.lightTheme,
      initialRoute: '/',
      routes: {
        '/': (context) => const DatePickerScreen(),
      },
);

date_picker_scrren.dart

class _DatePickerScreenState extends State<DatePickerScreen> {
      static const platform = MethodChannel('com.izan.abc');
      String dataShared = 'Initial value';
    
      @override
      void initState() {
        super.initState();
        getSharedText();
      }
    
      Future<void> getSharedText() async {
          var sharedData = await platform.invokeMethod('mymethod');
          if (sharedData != null) {
            setState(() {
              dataShared = sharedData;
            });
          }
    
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(body: Center(child: Text(dataShared)));
      }
}

.android/app/src/main/AndroidManifest.xml

<application
        android:label="abc"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>

        </activity>

        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
</application>

.android/app/src/main/java/com/izan/abc/host/MainActivity.java

public class MainActivity extends FlutterActivity {

    private String sharedText;
    private static final String CHANNEL = "com.izan.abc";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                handleSendText(intent);
            }
        }
    }

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);

        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler(
                        (call, result) -> {
                            if (call.method.contentEquals("mymethod")) {
                                result.success(sharedText);
                                sharedText = null;
                            }
                        }
                );
    }

    void handleSendText(Intent intent) {
        sharedText = intent.getStringExtra("myKey");
    }
}
  • Output on screen: Initial value
  • Expected output: From Native Intent

No communication is being established and the following logs are thrown:

E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: MissingPluginException(No implementation found for method mymethod on channel com.izan.abc)
    #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7)
    <asynchronous suspension>
    #1      _DatePickerScreenState.getSharedText (abc/view/date_picker_screen.dart:30:24)
    <asynchronous suspension>

Solutions tried:

  • flutter clean (have to edit files inside .android folder everytime I run this)
  • Invalidate cache and restart to native app
  • set both minifyEnabled and shrinkResources to false
    in app/build.gradle
Izan Majeed
  • 49
  • 10
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jun 03 '22 at 05:09
  • Your code tries to run a method named "mymethod", with this line in "date_picker_scrren.dart": `await platform.invokeMethod('mymethod')`. The error you posted states: "No implementation found for method mymethod on channel com.izan.abc". So, implement the method? – Kaan Jun 03 '22 at 15:39
  • @kaan as per documentation we've to override ```configureFlutterEngine``` inside MainActivity.java where we check the invoked method with the help of ```MethodCallHandler``` – Izan Majeed Jun 03 '22 at 15:50

0 Answers0