在插件开发期间,Flutter 看不到我的原生通道绑定

时间:2021-03-11 18:52:23

标签: android flutter flutter-android

我在为 android SDK 创建插件时遇到以下问题

package com.me.plugin

import android.app.Activity
import android.content.ContentValues.TAG
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.util.Log
import androidx.annotation.NonNull
import com.me.plugins.meplugin.Api
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel


class MainActivity: FlutterPlugin, MethodChannel.MethodCallHandler{


  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private lateinit var channel : MethodChannel
  private lateinit var context: Context
  private lateinit var activity: Activity

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    Api.getInstance()
    channel= MethodChannel(flutterPluginBinding.binaryMessenger,"net.plzft.poc_od")
    context = flutterPluginBinding.applicationContext
    channel.setMethodCallHandler(this)

  }

  override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
    if (call.method == "getPlatformVersion") {
      result.success("Android ${android.os.Build.VERSION.RELEASE}")
    }
    if (call.method.equals("isRegisteredToBackend_p")) {
      result.success(Api.getInstance().isRegisteredToBackend)
    }else {
            result.notImplemented()
          }
  }
}
import 'dart:async';

import 'package:flutter/services.dart';

class Meplugin {
  static const MethodChannel _channel =
      const MethodChannel('net.plzft.poc_od');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<bool> get isRegisteredToBackend__p async {
    final bool version = await _channel.invokeMethod('isRegisteredToBackend_p');
    print(version);
    return version;
  }
}
import 'dart:developer';

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:meplugin/meplugin.dart';
import 'package:shared_preferences/shared_preferences.dart';

class InitPage extends StatefulWidget {
  @override
  InitPageState createState() => InitPageState();
}

class InitPageState extends State<InitPage> {
  @override
  void initState() {
    super.initState();

    setState(() {
      _isRegisteredToBackend = null;
    });

    initIsRegisteredToBackend();
    initVibrateState();
  }

  void initVibrateState() async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setInt('notifications', 1);
  }

  //static const platform = const MethodChannel('net.plzft.poc_od');

  bool _isRegisteredToBackend = false;

  bool isRegisteredToBackend() {
    return _isRegisteredToBackend;
  }

  Future<void> initIsRegisteredToBackend() async {
    bool temp;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      temp = await meplugin.isRegisteredToBackend__p;
    } on PlatformException {
      temp = false;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    if (!temp && _isRegisteredToBackend != temp) {
      log('_isRegisteredToBackend: $temp');
      setState(() {
        _isRegisteredToBackend = temp;
      });
      Navigator.popAndPushNamed(context, '/welcome_page');
    }

    if (temp) {
      Navigator.popAndPushNamed(context, '/home_page');
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            new CircularProgressIndicator(),
          ],
        ),
      ),
    );
  }
}
Launching lib/main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
E/flutter (16050): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method isRegisteredToBackend on channel net.plzft.poc_od)
E/flutter (16050): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:157:7)
E/flutter (16050): <asynchronous suspension>
E/flutter (16050): #1      Logitmeplugin.isRegisteredToBackend (package:logitmeplugin/logitmeplugin.dart:16:26)
E/flutter (16050): <asynchronous suspension>
E/flutter (16050): #2      InitPageState.initIsRegisteredToBackend (package:logitmeplugin_example/init_page.dart:44:14)
E/flutter (16050): <asynchronous suspension>
E/flutter (16050): 
E/flutter (16050): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)

如您所见,我尝试创建通道,该通道应该发送一个 bool 作为对来自 flutter 的 request(methodcall) 的回答。 1.我在api中有一个方法isregisteredtobackend 2.我在调用它的 MainActivity 中使用它 3.我在lib源码中的插件中实现了这个方法 4.在init页面调用 5.找不到方法

我已经花了很多时间来找到我的问题所在...... 我不知道为什么 flutter 没有看到我的方法实现

0 个答案:

没有答案