Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR DART

FlutterError (Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.)

// 1. Separate widget : this example MyHome() out of MaterialApp()

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen()
    );
  }
}

// Separate HomeScreen()
class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
        child: ElevatedButton(
          child: Text("Click Me"),
          onPressed: () => Navigator.pushNamed(context, "/"),
        ),
      );
  }
}

// 2. Use Builder:
// Wrap with Builder if use same in MaterialApp()

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Builder(					// Builder:
        builder: (context) => Center(
              child: ElevatedButton(
                child: Text("Click Me"),
                onPressed: () => Navigator.pushNamed(context, "/"),
              ),
            ),
      ),
    );
  }
}

Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #FlutterError #operation #requested #context #include #The #context #push #pop #routes #Navigator #widget #descendant #Navigator
ADD COMMENT
Topic
Name
6+4 =