I got problem with GeneratedPluginRegistrant.registerWith method and MethodChannel object while using flutter

Chanwoo Timothy Lee picture Chanwoo Timothy Lee · Dec 15, 2019 · Viewed 20.8k times · Source

I'm trying the example here https://flutter.dev/docs/development/platform-integration/platform-channels#step-1-create-a-new-app-project

But it doesn't work if I type exactly same code. (I'm using Kotlin)

When I try

GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, CHANNEL)

The error said

Type mismatch: inferred type is MainActivity but FlutterEngine was expected

Unresolved reference: flutterView

So I've tried below

GeneratedPluginRegistrant.registerWith(FlutterEngine(this))
MethodChannel(FlutterView(this), CHANNEL)

It makes error disappeared but the other problem shows up

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method getBatteryLevel on channel samples.flutter.io/battery)

though getBatteryLevel() method exists

    private fun getBatteryLevel(): Int {
        val batteryLevel: Int
        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP){
            val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
            batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
        } else  {
            val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
            batteryLevel = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100 / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        }
        return batteryLevel
    }

I also tried getFlutterView(), not working.

Here's the point I want to know.

  1. Did error from GeneratedPluginRegistrant and MethodChannel solved well? I mean, is that a right way to fix? if not, what to input as an argument?
  2. I have no idea what to do following the error MissingPluginException. Should I build the plugin? or is there a way to solve this problem?

Here is full code and errors

main.dart

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  static const platform = const MethodChannel('samples.flutter.io/battery');
  String _batteryLevel = 'Unknown battery level. ';

  Future<void> _getBatteryLevel() async {
    String batteryLevel;
    try {
      final int result = await platform.invokeMethod('getBatteryLevel');
      batteryLevel = 'Battery level at $result % .';
    } on PlatformException catch (e) {
      batteryLevel = "Failed to get battery level: '${e.message}'.";
    }

    setState(() {
      _batteryLevel = batteryLevel;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('Get Battery Level'),
              onPressed: _getBatteryLevel,
            ),
            Text(
              _batteryLevel,
            ),
          ],
        ),
      ),
    );
  }
}

MainActivity.kt

package com.example.native_test


import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Bundle
import io.flutter.Log
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.view.FlutterView
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES



class MainActivity: FlutterActivity() {

    //private val CHANNEL = "plugins.flutter.io/battery"
    private val CHANNEL = "samples.flutter.io/battery"
    //private val CHANNEL = "com.example.batterylevel/battery"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i("user_debug", "MainActivity: onCreate")

        GeneratedPluginRegistrant.registerWith(FlutterEngine(this))
        MethodChannel(FlutterView(this), CHANNEL).setMethodCallHandler { call, result ->
            if (call.method == "getBatteryLevel") {
                val batteryLevel : Int = getBatteryLevel()

                if (batteryLevel != -1) {
                    result.success(batteryLevel)
                } else {
                    result.error("UNAVAILABLE", "Battery level not available.", null)
                }
            } else {
                Log.e("user_debug", "getBatteryLevel is not method of called")
                result.notImplemented()
            }
        }
    }

    private fun getBatteryLevel(): Int {
        val batteryLevel: Int
        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP){
            val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
            batteryLevel = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
        } else  {
            val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
            batteryLevel = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100 / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        }
        return batteryLevel
    }
}
Launching lib/main.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
E/flutter (23141): [ERROR:flutter/shell/common/shell.cc(197)] Dart Error: Unhandled exception:
E/flutter (23141): NoSuchMethodError: No top-level getter '' declared.
E/flutter (23141): Receiver: top-level
E/flutter (23141): Tried calling: 
E/flutter (23141): #0      NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:202:5)
E/flutter (23141): [ERROR:flutter/runtime/dart_isolate.cc(461)] Could not resolve main entrypoint function.
E/flutter (23141): [ERROR:flutter/shell/common/engine.cc(205)] Could not run the isolate.
E/flutter (23141): [ERROR:flutter/shell/common/engine.cc(137)] Engine not prepare and launch isolate.
E/flutter (23141): [ERROR:flutter/shell/common/shell.cc(197)] Dart Error: Unhandled exception:
E/flutter (23141): NoSuchMethodError: No top-level getter '' declared.
E/flutter (23141): Receiver: top-level
E/flutter (23141): Tried calling: 
E/flutter (23141): #0      NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:202:5)
E/flutter (23141): [ERROR:flutter/runtime/dart_isolate.cc(461)] Could not resolve main entrypoint function.
E/flutter (23141): [ERROR:flutter/shell/common/engine.cc(205)] Could not run the isolate.
E/flutter (23141): [ERROR:flutter/shell/common/engine.cc(137)] Engine not prepare and launch isolate.


...


E/flutter (23861): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method getBatteryLevel on channel samples.flutter.dev/battery)
E/flutter (23861): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7)
E/flutter (23861): <asynchronous suspension>
E/flutter (23861): #1      _MyHomePageState._getBatteryLevel (package:native_test/main.dart:55:41)
E/flutter (23861): #2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (23861): #3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (23861): #4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (23861): #5      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (23861): #6      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (23861): #7      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:199:7)
E/flutter (23861): #8      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:467:9)
E/flutter (23861): #9      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
E/flutter (23861): #10     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:117:9)
E/flutter (23861): #11     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (23861): #12     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:115:18)
E/flutter (23861): #13     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:7)
E/flutter (23861): #14     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (23861): #15     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (23861): #16     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (23861): #17     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (23861): #18     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (23861): #19     _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (23861): #20     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (23861): #21     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (23861): #22     _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (23861): #23     _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter (23861): 
E/flutter (23861): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method getBatteryLevel on channel samples.flutter.dev/battery)


...


E/flutter (  770): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7)
E/flutter (  770): <asynchronous suspension>
E/flutter (  770): #1      _MyHomePageState._getBatteryLevel (package:native_test/main.dart:56:41)
E/flutter (  770): #2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter (  770): #3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter (  770): #4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (  770): #5      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter (  770): #6      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter (  770): #7      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:199:7)
E/flutter (  770): #8      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:467:9)
E/flutter (  770): #9      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12)
E/flutter (  770): #10     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:117:9)
E/flutter (  770): #11     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (  770): #12     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:115:18)
E/flutter (  770): #13     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:7)
E/flutter (  770): #14     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (  770): #15     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (  770): #16     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (  770): #17     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (  770): #18     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (  770): #19     _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter (  770): #20     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter (  770): #21     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter (  770): #22     _invoke1 (dart:ui/hooks.dart:273:10)
E/flutter (  770): #23     _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter (  770): 
✓ Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk...
Syncing files to device Android SDK built for x86...

Answer

user12880293 picture user12880293 · Feb 11, 2020

Add <meta-data android:name="flutterEmbedding" android:value="2"/> to the AndroidManifest.xml.