Flutter based Android app throwing MissingPluginException(No implementation found for method on channel)

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

Flutter based Android app throwing MissingPluginException(No implementation found for method on channel)

问题

我正在尝试使我 Android 应用的原生端与 Flutter 进行通信。

这是在 Flutter 端的代码:

```dart
static const platform = const MethodChannel('getHallInfo');

@override
void initState() {
  testing().then((String lst) {
    setState(() {
      if (lst != null) str = lst;
    });
  });

  super.initState();
}

@override
Widget build(BuildContext context) {
  return Column(
    children: <Widget>[Text(str)],
  );
}

static Future<String> testing() async {
  String lst;
  try {
    lst = await platform.invokeMethod('getHalls');
  } catch (e) {
    print(e.toString());
  }

  return lst;
}

这是在原生 Java 端的代码:

private static final String CHANNEL = "getHallInfo";
super.configureFlutterEngine(getFlutterEngine());
GeneratedPluginRegistrant.registerWith(getFlutterEngine());

new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
  @Override
  public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {

    if (methodCall.method.equals("getHalls")) {
      String mm = "Succeeded";
      System.out.println("Entered");
      result.success(mm);
    }

  }
});

我的 Pubspec.yaml 文件内容:

name: flutterModule

description: A new flutter module project.

version: 1.0.0+1

environment:
 sdk: ">=2.1.0 <3.0.0"

dependencies:
 flutter:
  sdk: flutter

cupertino_icons: ^0.1.3

flutter_svg: ^0.17.4

google_fonts: ^1.1.0

intl: ^0.16.1

dev_dependencies:
 flutter_test:
  sdk: flutter

flutter:
 uses-material-design: true
assets:
- assets/icons/

module:
 androidX: true
 androidPackage: com.example.flutterModule
 iosBundleIdentifier: com.example.flutterModule

这是我遇到的错误:

MissingPluginException(No implementation found for method getHalls on channel getHallInfo)

注意: 终端上显示了 "Entered"

编辑: 我尝试过以下事项:

  1. 运行 flutter clean
  2. 运行 flutter upgrade
  3. 重启应用

但这个异常仍然存在。


<details>
<summary>英文:</summary>

I am trying to make the native side of my android app to communicate with Flutter.

This is on the Flutter side:

    static const platform = const MethodChannel(&#39;getHallInfo&#39;);

    @override
    void initState() {
    testing().then((String lst) {
      setState(() {
        if (lst != null) str = lst;
      });
    });

    super.initState();
    }

    @override
    Widget build(BuildContext context) {
    return Column(
      children: &lt;Widget&gt;[Text(str)],
    );
    }

    static Future&lt;String&gt; testing() async {
    String lst;
    try {
      lst = await platform.invokeMethod(&#39;getHalls&#39;);
    } catch (e) {
      print(e.toString());
    }

    return lst;
    }

And this is on the native Java:
    
    private static final String CHANNEL = &quot;getHallInfo&quot;;
    super.configureFlutterEngine(getFlutterEngine());
    GeneratedPluginRegistrant.registerWith(getFlutterEngine());

    new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {

                if (methodCall.method.equals(&quot;getHalls&quot;)) {
                  String mm= &quot;Succeeded&quot;;
                  System.out.println(&quot;Entered&quot;);
                    result.success(mm);
                }

            }
        });


My Pubspec.yaml:

    name: flutterModule
  
    description: A new flutter module project.

    version: 1.0.0+1

    environment:
     sdk: &quot;&gt;=2.1.0 &lt;3.0.0&quot;

    dependencies:
     flutter:
      sdk: flutter

    cupertino_icons: ^0.1.3

    flutter_svg: ^0.17.4

    google_fonts: ^1.1.0

    intl: ^0.16.1
 
    dev_dependencies:
     flutter_test:
      sdk: flutter

    flutter:
     uses-material-design: true
    assets:
    - assets/icons/
 
    module:
     androidX: true
     androidPackage: com.example.flutterModule
     iosBundleIdentifier: com.example.flutterModule


This is the error I get:

&gt; MissingPluginException(No implementation found for method getHalls on channel getHallInfo)

**Note:** &quot;Entered&quot; is shown on the terminal

**Edit:** These are the things that I tried:

1. Run flutter clean
2. Run flutter upgrade
3. Restart the app

But this exception persists. 

</details>


# 答案1
**得分**: 1

你只需要停止应用然后重新启动它。

确保在停止应用之前执行了 `pub get`。

使用以下命令:

```bash
flutter pub get
英文:

you just have to stop the app and restart it again.

make sure you did pub get before you stop your app.
use the command .

flutter pub get

答案2

得分: 1

尝试运行 flutter clean

然后运行 flutter upgrade

接着运行 flutter run -v

如果这不起作用,请查看以下问题:

issue01
issue02

希望这能帮到您。

英文:

Try flutter clean

then flutter upgrade

then flutter run -v

If that doesn't work, have a look at the following issues:
issue01,
issue02

I hope this will help you

答案3

得分: 0

你只需要运行

Flutter clean

然后再重新启动/运行你的应用程序。

英文:

you just need to run

Flutter clean

then again restart/run your app

huangapple
  • 本文由 发表于 2020年7月26日 16:26:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63097819.html
匿名

发表评论

匿名网友

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

确定