navigatorKey property

GlobalKey<NavigatorState>? navigatorKey
final

A key to use when building the Navigator.

In tests, this allows direct access to the NavigatorState for programmatic navigation without needing to find the Navigator widget:

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('widgets app can use navigator test', (WidgetTester tester) async {
    final navigatorKey = GlobalKey<NavigatorState>();

    await tester.pumpWidget(TestWidgetsApp(
      navigatorKey: navigatorKey,
      home: const Text('Home'),
    ));

    navigatorKey.currentState!.pushNamed('/details');
  });
}

See also:

Implementation

final GlobalKey<NavigatorState>? navigatorKey;