If above solution does not works please try this solution.
-
Create the class. Here it named as
NavigationServiceimport 'package:flutter/material.dart'; class NavigationService { static GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); } -
Set the navigatorKey property of MaterialApp in the main.dart
Widget build(BuildContext context) { return MaterialApp( navigatorKey: NavigationService.navigatorKey, // set property ) } -
Great! Now you can use anywhere you want e.g.
print("---print context: ${NavigationService.navigatorKey.currentContext}");