//2.本地kotlin代码class MainActivity : FlutterActivity() {
companion object {
const val FLUTTER_ANDROID_LOG_CHANNEL = "flutter_android_log"
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, FLUTTER_ANDROID_LOG_CHANNEL)
.setMethodCallHandler { call, result ->
var tag: String = call.argument("tag") ?: "LogUtils"
var message: String = call.argument("msg") ?: "unknown log message"
when (call.method) {
"logD" -> Log.d(tag, message)
"logE" -> Log.e(tag, message)
}
result.success(null)
}
}
}
//2.flutter的dart代码 下划线_表示私有变量import ‘package:flutter/services.dart‘;class LogUtils { static const String _tag = "LogUtils"; static const _perform = const MethodChannel("flutter_android_log"); static void d(String message) { _perform.invokeMethod("logD", {‘tag‘: _tag, ‘msg‘: message}); } static void e(String message) { _perform.invokeMethod("logE", {‘tag‘: _tag, ‘msg‘: message}); }}//3.调用
void _onPush() { LogUtils.e("_onPush press");}注意坑:与native方法有交互时,最好先Stop掉,再Start。热启动很多时候无效,坑死了。。。