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

huangapple 未分类评论65阅读模式
英文:

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

问题

  1. 我正在尝试使我 Android 应用的原生端与 Flutter 进行通信。
  2. 这是在 Flutter 端的代码:
  3. ```dart
  4. static const platform = const MethodChannel('getHallInfo');
  5. @override
  6. void initState() {
  7. testing().then((String lst) {
  8. setState(() {
  9. if (lst != null) str = lst;
  10. });
  11. });
  12. super.initState();
  13. }
  14. @override
  15. Widget build(BuildContext context) {
  16. return Column(
  17. children: <Widget>[Text(str)],
  18. );
  19. }
  20. static Future<String> testing() async {
  21. String lst;
  22. try {
  23. lst = await platform.invokeMethod('getHalls');
  24. } catch (e) {
  25. print(e.toString());
  26. }
  27. return lst;
  28. }

这是在原生 Java 端的代码:

  1. private static final String CHANNEL = "getHallInfo";
  2. super.configureFlutterEngine(getFlutterEngine());
  3. GeneratedPluginRegistrant.registerWith(getFlutterEngine());
  4. new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
  5. @Override
  6. public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
  7. if (methodCall.method.equals("getHalls")) {
  8. String mm = "Succeeded";
  9. System.out.println("Entered");
  10. result.success(mm);
  11. }
  12. }
  13. });

我的 Pubspec.yaml 文件内容:

  1. name: flutterModule
  2. description: A new flutter module project.
  3. version: 1.0.0+1
  4. environment:
  5. sdk: ">=2.1.0 <3.0.0"
  6. dependencies:
  7. flutter:
  8. sdk: flutter
  9. cupertino_icons: ^0.1.3
  10. flutter_svg: ^0.17.4
  11. google_fonts: ^1.1.0
  12. intl: ^0.16.1
  13. dev_dependencies:
  14. flutter_test:
  15. sdk: flutter
  16. flutter:
  17. uses-material-design: true
  18. assets:
  19. - assets/icons/
  20. module:
  21. androidX: true
  22. androidPackage: com.example.flutterModule
  23. iosBundleIdentifier: com.example.flutterModule

这是我遇到的错误:

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

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

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

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

但这个异常仍然存在。

  1. <details>
  2. <summary>英文:</summary>
  3. I am trying to make the native side of my android app to communicate with Flutter.
  4. This is on the Flutter side:
  5. static const platform = const MethodChannel(&#39;getHallInfo&#39;);
  6. @override
  7. void initState() {
  8. testing().then((String lst) {
  9. setState(() {
  10. if (lst != null) str = lst;
  11. });
  12. });
  13. super.initState();
  14. }
  15. @override
  16. Widget build(BuildContext context) {
  17. return Column(
  18. children: &lt;Widget&gt;[Text(str)],
  19. );
  20. }
  21. static Future&lt;String&gt; testing() async {
  22. String lst;
  23. try {
  24. lst = await platform.invokeMethod(&#39;getHalls&#39;);
  25. } catch (e) {
  26. print(e.toString());
  27. }
  28. return lst;
  29. }
  30. And this is on the native Java:
  31. private static final String CHANNEL = &quot;getHallInfo&quot;;
  32. super.configureFlutterEngine(getFlutterEngine());
  33. GeneratedPluginRegistrant.registerWith(getFlutterEngine());
  34. new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
  35. @Override
  36. public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
  37. if (methodCall.method.equals(&quot;getHalls&quot;)) {
  38. String mm= &quot;Succeeded&quot;;
  39. System.out.println(&quot;Entered&quot;);
  40. result.success(mm);
  41. }
  42. }
  43. });
  44. My Pubspec.yaml:
  45. name: flutterModule
  46. description: A new flutter module project.
  47. version: 1.0.0+1
  48. environment:
  49. sdk: &quot;&gt;=2.1.0 &lt;3.0.0&quot;
  50. dependencies:
  51. flutter:
  52. sdk: flutter
  53. cupertino_icons: ^0.1.3
  54. flutter_svg: ^0.17.4
  55. google_fonts: ^1.1.0
  56. intl: ^0.16.1
  57. dev_dependencies:
  58. flutter_test:
  59. sdk: flutter
  60. flutter:
  61. uses-material-design: true
  62. assets:
  63. - assets/icons/
  64. module:
  65. androidX: true
  66. androidPackage: com.example.flutterModule
  67. iosBundleIdentifier: com.example.flutterModule
  68. This is the error I get:
  69. &gt; MissingPluginException(No implementation found for method getHalls on channel getHallInfo)
  70. **Note:** &quot;Entered&quot; is shown on the terminal
  71. **Edit:** These are the things that I tried:
  72. 1. Run flutter clean
  73. 2. Run flutter upgrade
  74. 3. Restart the app
  75. But this exception persists.
  76. </details>
  77. # 答案1
  78. **得分**: 1
  79. 你只需要停止应用然后重新启动它。
  80. 确保在停止应用之前执行了 `pub get`。
  81. 使用以下命令:
  82. ```bash
  83. 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 .

  1. 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

你只需要运行

  1. Flutter clean

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

英文:

you just need to run

  1. Flutter clean

then again restart/run your app

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

发表评论

匿名网友

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

确定