【问题标题】:Flutter MethodChannel from android to flutter not working从android到flutter的Flutter MethodChannel不起作用
【发布时间】: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


    【解决方案1】:

    我尝试按照某些示例中的建议添加等待,但它也会引发错误我仍在尝试顺便说一句

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 1970-01-01
      • 2022-01-14
      • 2020-06-16
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 2021-08-05
      • 2021-03-18
      相关资源
      最近更新 更多