Flutter – Native Android Component invoke with MethodChannel only works on first time then throws MissingPluginException

huangapple go评论73阅读模式
英文:

Flutter - Native Android Component invoke with MethodChannel only works on first time then throws MissingPluginException

问题

I have a Flutter app with a native android component for scanning barcodes, on first run this native component runs fine and does what it needs to then returns to the Flutter section of app.

然后,任何时候,如果尝试再次打开本地 Android 组件,它总是会抛出错误:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method StartSecondActivity on channel com.example)

Only restarting the app seems to solve this problem, where it will run once with no error then repeat.

仅重新启动应用似乎可以解决这个问题,然后它会再次运行而不会出错。

Dart 代码看起来是这样的,第一次运行正常,但随后的尝试会报错,除非重新启动应用:

MethodChannel _channel = MethodChannel('com.example');
await _channel.invokeMethod('StartSecondActivity');

MainActivity.java 类:

import android.content.Intent;
import android.widget.Toast;

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example";

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

    GeneratedPluginRegistrant.registerWith(flutterEngine);

    flutterEngine.getNavigationChannel().setInitialRoute("/waybill");

    FlutterEngineCache.getInstance().put("bac_flutter_id", flutterEngine);

    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
        .setMethodCallHandler((call, result) -> {
            if(call.method.equals("StartSecondActivity")){

                Intent intent = new Intent(MainActivity.this, IntentWedgeSample.class);
                startActivity(intent);
                //result.success("ActivityStarted");
            }else{
                //result.notImplemented();
            }
        });
    }
}

Fairly new to Flutter and did not originally write this app so any hints on how to go about solving this would be much appreciated.

对Flutter相对较新,原本并不是这个应用的作者,所以对如何解决这个问题的任何提示将不胜感激。

英文:

I have a Flutter app with a native android component for scanning barcodes, on first run this native component runs fine and does what it needs to then returns to the Flutter section of app.

However, anytime after that if you try to open the native android component again it always throws the error: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method StartSecondActivity on channel com.example)

Only restarting the app seems to solve this problem, where it will run once with no error then repeat.

dart looks something like this, runs first time and gets error any subsequent tries unless app is restarted:

    MethodChannel _channel = MethodChannel('com.example');
    await _channel.invokeMethod('StartSecondActivity');

MainActivity.java class:

    import android.content.Intent;
    import android.widget.Toast;

    import androidx.annotation.NonNull;
    import io.flutter.embedding.android.FlutterActivity;
    import io.flutter.embedding.engine.FlutterEngine;
    import io.flutter.embedding.engine.FlutterEngineCache;
    import io.flutter.plugin.common.MethodChannel;
    import io.flutter.plugins.GeneratedPluginRegistrant;

    public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "com.example";

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

        GeneratedPluginRegistrant.registerWith(flutterEngine);

        flutterEngine.getNavigationChannel().setInitialRoute("/waybill");

        FlutterEngineCache.getInstance().put("bac_flutter_id", flutterEngine);

        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
            .setMethodCallHandler((call, result) -> {
                if(call.method.equals("StartSecondActivity")){

                    Intent intent = new Intent(MainActivity.this, IntentWedgeSample.class);
                    startActivity(intent);
                    //result.success("ActivityStarted");
                }else{
                    //result.notImplemented();
                }
            });
        }
    }

Fairly new to Flutter and did not originally write this app so any hints on how to go about solving this would be much appreciated.

答案1

得分: 0

I managed to fix this by using Android Intent+ plugin instead of MethodChannel (https://pub.dev/packages/android_intent_plus)

Then instead of the MethodChannel code, use:

AndroidIntent intent = AndroidIntent(
action: 'android.intent.action.ACTION_VIEW',

// Replace this by your package name.
package: 'com.example',

// Replace this by your package name followed by the activity you want to open.
// The default activity provided by Flutter is MainActivity, but you can check
// this in AndroidManifest.xml.
componentName: 'com.example.IntentWedgeSample',
);

await intent.launch();

英文:

Managed to fix this by using Android Intent+ plugin instead of MethodChannel (https://pub.dev/packages/android_intent_plus)

Then instead of the MethodChannel code, use:

           AndroidIntent intent = AndroidIntent(
            action: 'android.intent.action.ACTION_VIEW',

            // Replace this by your package name.
            package: 'com.example',

            // Replace this by your package name followed by the activity you want to open.
            // The default activity provided by Flutter is MainActivity, but you can check
            // this in AndroidManifest.xml.
            componentName: 'com.example.IntentWedgeSample',
          );

          await intent.launch();

huangapple
  • 本文由 发表于 2023年2月27日 19:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定