Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Created April 18, 2024 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredgrott/087cd8330491a0963185a1611c9646cf to your computer and use it in GitHub Desktop.
Save fredgrott/087cd8330491a0963185a1611c9646cf to your computer and use it in GitHub Desktop.
app globals
import 'package:flutter/material.dart';
final navigatorKey = GlobalKey<NavigatorState>();
// navigatorKey is parameter which goes in the MaterialApp
// navigagtorKey parameter slot. Then we can do
// LocalizationClassSingeton.of(appContect)!.loremIpsum
// in places outside the widgets such as the Models.
// That also means that to test the models we use a MaterialApp
// wrapper in test to pumpwidget with child widget being
// anything to insert the navigatorKey parameter and
// localizations as then after the pump of widgets we
// have the appContext global filled and then can
// test the model stuff that has localizations.
//
BuildContext appContext = navigatorKey.currentState!.context;
// if using routes u need something different
// scaffoldMessengerKey is used in MaterialApp.routes
final scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
// Then
BuildContext routeAppContext = scaffoldMessengerKey.currentContext!;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment