当前位置: 首页 > 知识库问答 >
问题:

无法解决颤振本机通信错误-未处理的异常在通道上找不到方法的实现

易京
2023-03-14

我刚刚从颤振中复制了代码。dev并对其进行了修改,出现以下错误

E/flatter(2639):[ERROR:flatter/lib/ui/ui\u dart\u state.cc(177)]未处理的异常:MissingPluginException(在通道My channel上找不到方法myNativeFunction的实现)*我想将一条消息从本机java代码打印到Flatter 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";
    }
}

共有1个答案

徐秋月
2023-03-14

Main Active中,方法通道名称应与省道中定义的方法通道名称匹配。

飞镖

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"
 类似资料: