Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

callback - How can I access flutter app context when I call CallbackHandler from background?

I built a background service to invoke a flutter function when the app in the background using

var callbackHandle = PluginUtilities.getCallbackHandle(backgroundHandler);
channel.invokeMethod('startService', callbackHandle.toRawHandle());

now I want to view an alert dialog when I call this callback function, but I can't access the context, how can I view a dialog or navigate from flutter background function?

question from:https://stackoverflow.com/questions/65889475/how-can-i-access-flutter-app-context-when-i-call-callbackhandler-from-background

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If the flutter app is actually running you can access the context via a navigatorKey on the MaterialApp. Note that the function you are referring to needs to be in the dart side in the flutter app.

final navigatorKey = GlobalKey<NavigatorState>();

Widget build(BuildContext context) {
    return MaterialApp(
       navigatorKey: navigatorKey
    );
}

Then in the flutter function you can gain access to the context by navigatorKey.currentState.context or navigatorKey.currentState.overlay.context


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...