Flutter - how to get current context?

前端 未结 3 377
刺人心
刺人心 2021-01-18 20:48

I am using Firebase cloud messaging for notifications, and i want to show a dialog or snackbar once i receive a notification when i am inside the application, my problem is

相关标签:
3条回答
  • 2021-01-18 21:17

    do the initializing inside a build method of your first widget in the tree ! which normally it called an App widget and it is StateLess StateFull widget and inside the build method you have access to the BuildContext

    0 讨论(0)
  • 2021-01-18 21:22

    I had the exact same issue, but I found a brilliant thread on GitHub. Basically, you can create a navigatorKey and pass that in to MaterialApp, and then use that navigatorKey to change route.

    See how in this thread: https://github.com/brianegan/flutter_redux/issues/5#issuecomment-361215074

    0 讨论(0)
  • 2021-01-18 21:28

    I ended up using Overlay support:

    https://pub.dev/packages/overlay_support

    It is basically called at the very beginning of my tree just like wrapping providers at the main.dart, it worked like a charm, nothing else worked at all! Also here is a tutorial that helped me a lot:

    https://medium.com/flutter-community/in-app-notifications-in-flutter-9c1e92ea10b3

    0 讨论(0)
提交回复
热议问题