请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师,怎么老是提示在通道中找不到实现的方法,我应该怎么改代码呢?谢谢!

MissingPluginException(No implementation found for method test on channel com.flutter_demo/methodPlugin)

Dart:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class PluginTest extends StatefulWidget {
  @override
  _PluginState createState() => _PluginState();
}

class _PluginState extends State<PluginTest> {

  static const MethodChannel _methodChannel = MethodChannel('com.flutter_demo/methodPlugin');

  String _init = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('调用方法'),
      ),
      body: Column(
        children: <Widget>[
          OutlineButton(
            onPressed: () {
              _callMethod();
            },
            child: Text('调用方法'),
          ),
          Text(_init)
        ],
      ),
    );
  }

  void _callMethod() async {
    Future<String> future = await _methodChannel.invokeMethod('test');
    future.then((value){
      setState(() {
        _init = value;
      });
    });

  }
}

Android:

package com.flutter_demo;

import android.os.Bundle;
import android.util.Log;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);

        MethodPlugin.registerWith(this);
    }


}




class MethodPlugin implements MethodCallHandler {


    private static final String channel = "com.flutter_demo/methodPlugin";

    static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }

        MethodPlugin.registerWith(registry.registrarFor(channel));

    }

    private static void registerWith(Registrar registrar) {
        MethodChannel channel =
                new MethodChannel(registrar.messenger(), "com.flutter_demo/methodPlugin");
        MethodPlugin instance = new MethodPlugin();
        channel.setMethodCallHandler(instance);
    }


    @Override
    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        Log.e("onMethodCall", methodCall.method);
        switch (methodCall.method) {
            case "Test":
                result.success(test());
                break;
            default:
                result.notImplemented();
                break;
        }

    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = GeneratedPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }

    private String test() {
        return "我是Native端的数据";
    }
}



求助,老师帮忙看下是什么原因造成的 ? 这里老是提示没实现方法,我是照着源码实现的还是不行。

正在回答 回答被采纳积分+3

2回答

上海小王思聪 2020-09-12 09:34:39

停止重新跑

0 回复 有任何疑惑可以回复我~
提问者 _呵呵哒_ 2019-05-16 17:44:13

已找到问题 , 嘿嘿 !!

0 回复 有任何疑惑可以回复我~
  • 什么原因?
    我也是报这个错
    回复 有任何疑惑可以回复我~ 2019-05-20 11:27:43
  • 提问者 _呵呵哒_ 回复 慕圣6074539 #2
    android代码没同步到flutter上,android代码修改后要 flutter attch 才行
    回复 有任何疑惑可以回复我~ 2019-05-21 15:36:12
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号