【问题标题】:Can't resolve flutter native communication error - Unhandled Exception No implementation found for method on Channel无法解决颤振原生通信错误 - 未处理的异常未找到通道上的方法的实现
【发布时间】:2021-05-21 18:38:03
【问题描述】:

我刚刚从 flutter.dev 复制代码并修改它并得到以下错误

E/flutter (2639): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] 未处理异常:MissingPluginException(未找到通道 My Channel 上的方法 myNativeFunction 的实现) * 我想将来自 Native java 代码的消息打印到 Flutter UI *

This is my Main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static const platform = const MethodChannel('My Channel');

  String message = "No Message from Native App";
  Future<void> callNative() async {
    String messageFromNative = "No message from Native";
    try {
      messageFromNative = await platform.invokeMethod('myNativeFunction');
      print(messageFromNative);
    } on PlatformException catch (e) {
      print("error + '${e.message}' ");
      message = "Failed to get Native App function: '${e.message}'.";
    }
    setState(() {
      message = messageFromNative;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Native Demo'),
        ),
        body: Center(
          child: Column(
            children: [
              Text(message),
              RaisedButton(child: Text('Native '), onPressed: callNative)
            ],
          ),
        ),
      ),
    );
  }
}
This is my MainActivity.java
package com.example.batterylevel;
import android.util.Log;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends FlutterActivity {

    public static final String CHANNEL="MyChannel";

   @Override
   public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
       super.configureFlutterEngine(flutterEngine);
       new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
               .setMethodCallHandler(
                       (call, result) -> {
                           
                           if(call.method.equals("myNativeFunction"))
                           {
                               String messageToFlutter=myNativeFunction();
                               result.success(messageToFlutter);
                           }else {
                               result.notImplemented();
                               Log.d("RD","else");
                           }

                       }
               );
   }

    String myNativeFunction()
    {
        return "Message from Android";
    }
}

【问题讨论】:

    标签: java android flutter cross-platform flutter-platform-channel


    【解决方案1】:

    MainActivity 中,方法通道名称应与dart 中定义的方法通道名称匹配。

    飞镖

    static const platform = const MethodChannel('My Channel');
    

    Java

    public static final String CHANNEL = "My Channel"; // on dart side it's "My Channel", but you've written "MyChannel"
    

    【讨论】:

    • 谢谢。在两个地方更改了名称,但错误仍然存​​在,请您帮忙。静态常量平台 = 常量 MethodChannel('channel'); public static final String CHANNEL="channel";
    • 如果平台端代码有任何更改,您需要重新构建应用程序并重新运行它。对本机代码进行更改时,热重载和热重启不起作用。
    猜你喜欢
    • 2021-12-21
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2020-10-22
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多