2

In android 10 clipboard return null but in others android version is ok and not problem.

This problem only exists when use clipboard in AppLifecycleState.resumed

my code:

@override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.resumed:
        ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);
        print("onResumed __${data}");
        break;
    }
  }
AhmadReza
  • 421
  • 6
  • 20

1 Answers1

1

We should access the clipboard in Window.Callback.onWindowFocusChanged(true), as that is the moment at which web gain input focus, which is required to read the clipboard in Android 10 (Q).

So for access to clipboard we need input focus in onResume.

I changed the code this way and my problem was solved:

@override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.resumed:
        FocusScope.of(context).requestFocus(new FocusNode()); // This line added
        ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);
        print("onResumed __${data}");
        break;
    }
  }
AhmadReza
  • 421
  • 6
  • 20