2

I have a Flutter app with 2 screens. First is "IntroPage" and second is "MainPage". I have run same channel and same code in these 2 screens for communicating to android native. But in MainPage everything works correctly, but in IntroPage when I invoke a method from android to flutter, setMethodCallHandler in flutter not working.

IntroPage and MainPage have same code:

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',
        ),
      ),
    );
  }
}

and simply I call invokeMethod in android layer like this in a handler:

Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
    methodChannel.invokeMethod("SELECT");
};
mainHandler.post(myRunnable);

Note that this problem only occurres in android 12, in other devices every thing works correctly.

Mohamadamin
  • 564
  • 5
  • 16

0 Answers0