Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...