该方式可以实现双向通道,Flutter调用原生,原生再返回数据给Flutter。
1.先在Flutter端声明MethodChannel的对象
class MethodChannelManage{
static const testPlatform = const MethodChannel("com.demo.test");
}
2.Flutter发送消息给原生
Map<String, String> map = {
"text": _methodChannelStr,
};
MethodChannelManage.testPlatform.invokeMethod("methodChanne", map)
.then((value) {// 原生返回的值
setState(() {
_methodChannelStr = value;
});
});
3.1Android接收和返回值
public class MainActivity extends FlutterActivity {
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
ShimPluginRegistry shimPluginRegistry = new ShimPluginRegi