我在这里尝试这个例子https://flutter.dev/docs/development/platform-integration/platform-channels#step-1-创建-a-新建-app-项目
但是如果我输入完全相同的代码,它就不起作用了。(我使用静态编程语言)
当我尝试时
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, CHANNEL)
错误是这样说的
类型不匹配:推断的类型是MainActive,但预计是FlutterEngine
未解析的引用:颤振视图
所以我在下面试过了
GeneratedPluginRegistrant.registerWith(FlutterEngine(this))
MethodChannel(FlutterView(this), CHANNEL)
它使错误消失,但另一个问题出现了
[错误:flutter/lib/用户界面/ui_dart_state.cc(157)]未处理的异常:MissingPluginException(未找到通道samples.flutter.io/battery方法getBatteryLevel的实现)
尽管getBatteryLevel()方法存在
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
}
我还尝试了GetFlatterView(),但不起作用。
这是我想知道的一点。
下面是完整的代码和错误
主要的飞奔
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,
),
],
),
),
);
}
}
主要活动。千吨级
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...
在我的情况下,我只需转到android/main/MainActivity。java和粘贴官方文档代码here=here
升级步骤=
老实说,我很生气,因为这个错误...
我不知道我的做法是否正确,但是。。。为了让它工作,我不得不将它改为:
GeneratedPluginRegistrant.registerWith(new FlutterEngine(this));
另外,等待平台。invokeListMethod('getBatteryLevel')
等待未来
所以我不得不更改java代码以返回列表。
已添加导入
import java.util.List;
将代码行更改为:
List<Integer> batteryLevel = getBatteryLevel();
在那个方法调用中,我将代码更改为:
private List<Integer> getBatteryLevel() {
List<Integer> result = new ArrayList<>();
int batteryLevel = -1;
...
result.add(batteryLevel);
return result;
}
在dart方面,还要更改代码,因为java现在返回整数列表
try {
final batteryLevel = await platform.invokeListMethod('getBatteryLevel');
setState(() {
_batteryLevel = batteryLevel[0];
});
添加<代码>
正在启动lib\main。IA Emulator上AOSP上的dart处于调试模式。。。正在运行Gradle任务“assembleDebug”。。。警告:插件路径_provider_android需要android SDK版本31。警告:插件共享的_首选项_android需要android SDK版本31。一个或多个插件需要更高的Android SDK版本。通过将以下内容添加到D:\App Dev
在我的flatter应用程序中,我正在使用firebase\u auth for google\u sign\u In,它运行良好。但是当我把cloud\u firestore添加到pubspec时。yaml然后获取依赖项,然后重新启动我的应用程序。它会显示以下错误:
我正在开发一个原生插件,并试图进行单元测试。所有单元测试都将在Dart中完成(无本机代码)。 Flutter有一个测试示例,说明如何使用测试从Dart到本机的调用方法通道。 问题是,我还没有找到测试从本机调用Dart的方法通道的方法,该Dart使用<code>setMethodCallHandler</code>来处理本机调用。 下面是一个示例
本文向大家介绍JavaScript中的call方法和apply方法使用对比,包括了JavaScript中的call方法和apply方法使用对比的使用技巧和注意事项,需要的朋友参考一下 方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象。 说明: call 方法可以用来代替另
问题内容: 对象a2的类型为A,但引用了类C的对象。因此,a2应该能够访问m3()。但是,为什么不发生呢?如果在类A中定义了m3()方法,则代码可以正常运行 问题答案: 这意味着您只能访问Class的成员和Class的实现(如果有的话)。 现在m3不是的成员。清楚吗?
这只有在我添加了足够多的依赖项时才会发生,这可能是预期的。我已经按照说明(https://developer.Android.com/studio/build/multidex.html)在Android项目文件中启用了multidex并添加了multidex依赖项,但不确定如何处理Flutter应用程序的“为multidex配置您的应用程序”中的第2步,甚至不确定是否遗漏了该步骤。 > 从工具栏