android - FlutterActivity MethodChannel和FlutterView

import io.flutter.embedding.engine.FlutterEngine

import io.flutter.plugin.common.MethodChannel

import io.flutter.plugins.GeneratedPluginRegistrant

import io.flutter.view.FlutterMain

class MainActivity : FlutterActivity() {

override fun configureFlutterEngine(flutterEngine: FlutterEngine) {

GeneratedPluginRegistrant.registerWith(flutterEngine);

}

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result -> // here is the error

if (call.method == “helloFromNativeCode”) {

val greetings = helloFromNativeCode()

result.success(greetings)

}

}

}

private fun helloFromNativeCode(): String {

return “Hello from Native Android Code”

}

companion object {

private const val CHANNEL = “flutter.native/helper”

}

}

我希望有一个人可以帮助我。

最佳答案

代替flutterView,请使用flutterEngine.getDartExecutor()。

关于android - FlutterActivity MethodChannel和FlutterView,我们在Stack Overflow上找到一个类似的问题: [https://stackoverflow.com/questions/59323505/](()

import ‘package:flutter/material.dart’;

import ‘package:flutter/services.dart’;

import ‘package:flutter_package_demo/theme/themes.dart’;

class UserPage extends StatefulWidget {

const UserPage({Key key}) : super(key: key);

@override

_UserPageState createState() => _UserPageState();

}

const String CHANNEL =

“www.luckly.work”; //这儿要与 MethodChannel(flutterView, CHANNEL)中CHANNEL名称一致

const String invokeMethod =

“breeze”; //这儿要与 call.method == invokeMethod中invokeMethod名称一致

var channel = co 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 nst MethodChannel(CHANNEL);

class _UserPageState extends State {

String _result = “”;

@override

Widget build(BuildContext context) {

final theme = Theme.of(context);

return Scaffold(

appBar: AppBar(

title: Text(

“个人”,

style: TextStyle(

color: theme.accentColor,

),

),

),

body: Container(

child: Column(

children: [

Text(

“个人”,

style: TextStyle(

color: theme.accentColor,

),

),

FlatButton(

onPressed: () {

setState(() {});

currentTheme.toggleTheme();

},

child: Text(

“多主题”,

style: TextStyle(

color: theme.accentColor,

),

),

),

Center(

child: RaisedButton(

onPressed: () async {

var result = await channel.invokeMethod(invokeMethod);

setState(() {

_result = result;

});

print(result);

},

child: new Text(_result),

),

)

],

),

),

);

}

}

package com.example.flutter_package_demo

//import io.flutter.app.FlutterActivity

import android.os.Bundle

import io.flutter.app.FlutterActivity

import io.flutter.plugin.common.MethodChannel

import io.flutter.plugins.GeneratedPluginRegistrant

//import io.flutter.embedding.android.FlutterActivity

//import io.flutter.view.FlutterMain

class MainActivity : FlutterActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

// GeneratedPluginRegistrant.registerWith(this) // here is the error: Type mismatch. Required: FlutterEngine! Found: MainActivity

MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->

if (call.method == InvokeMethod) {

val greetings = successNativeCode()

result.success(greetings)

}

}

}

private fun successNativeCode(): String {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值