Search
 
SCRIPT & CODE EXAMPLE
 

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, "/"),
              ),
            ),
      ),
    );
  }
}

Comment

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, "/"),
              ),
            ),
      ),
    );
  }
}

Comment

PREVIOUS NEXT
Code Example
Dart :: flutter inheritance 
Dart :: get HH:MM time in flutter 
Dart :: dart httop client 
Dart :: flutter leading 
Dart :: flutter phone direct caller 
Dart :: getting date from 12am dart 
Dart :: flutter component position absolute 
Dart :: flutter otp input scrren 
Dart :: lifecycle methods flutter 
Dart :: paste clipboard flutter 
Dart :: how to give width based on screen size flutter 
Dart :: get first word of a string before space flutter 
Dart :: dart remove from list 
Dart :: flutter logo flutter 
Dart :: dart exit function 
Dart :: catching a socket exception in flutter 
Dart :: use a class variable in other class in flutter 
Dart :: what is the difference between runapp() and main() in flutter 
Dart :: dart svg drawer 
Dart :: nullable conditional assignment dart 
Dart :: expand contanner in signlescroll flutter 
Dart :: convert data type dart 
Dart :: loob in dart 
Dart :: dart remove the last letter in a string 
Swift :: how to change the font of buttons programmatically swift 
Swift :: get device name swift 
Swift :: swift function with return value 
Swift :: swift struct field default value 
Swift :: swift http request header 
Swift :: replace character in swift 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =