Get the Global Context in Flutter

If above solution does not works please try this solution.

  1. Create the class. Here it named as NavigationService

    import 'package:flutter/material.dart';
    
    class NavigationService { 
      static GlobalKey<NavigatorState> navigatorKey = 
      GlobalKey<NavigatorState>();
    }
    
  2. Set the navigatorKey property of MaterialApp in the main.dart

    Widget build(BuildContext context) {
      return MaterialApp(
        navigatorKey: NavigationService.navigatorKey, // set property
      )
    }
    
  3. Great! Now you can use anywhere you want e.g.

    print("---print context: 
      ${NavigationService.navigatorKey.currentContext}");
    

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)