【发布时间】:2023-02-10 03:56:51
【问题描述】:
我有一个带有 2 个屏幕的 Flutter 应用程序。第一个是“IntroPage”,第二个是“MainPage”。 我在这两个屏幕中运行相同的通道和相同的代码以与 android 本机通信。 但是在 MainPage 中,一切正常,但是在 IntroPage 中,当我从 android 调用一个方法来颤动时,颤动中的 setMethodCallHandler 不起作用。
IntroPage 和 MainPage 具有相同的代码:
class IntroPage extends StatefulWidget {
const IntroPage({Key? key}) : super(key: key);
@override
_IntroPageState createState() => _IntroPageState();
}
class _IntroPageState extends State<IntroPage> {
@override
void initState() {
super.initState();
}
void select() async {
// this method not work correctly ...
AndroidChannel.androidChannel.setMethodCallHandler((call) async {
if (call.method == AndroidConstants.SELECT) {
debugPrint("here");
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Button(
onPressed: () {
select();
},
isActive: true,
title: 'Select',
),
),
);
}
}
我只是在处理程序中像这样在 android 层中调用 invokeMethod:
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
methodChannel.invokeMethod("SELECT");
};
mainHandler.post(myRunnable);
请注意,此问题仅发生在 android 12 中,在其他设备中一切正常。
【问题讨论】:
标签: java android flutter dart flutter-method-channel