skip to Main Content

Hi Im making a dummy test app in flutter using stepper everything going fine I tried making a button and I did make a button and everything was fine until i encountered Navigator.push error it wont let me go to the next screen

// ignore_for_file: no_logic_in_create_state, prefer_const_constructors, prefer_const_literals_to_create_immutables, use_super_parameters, prefer_const_constructors_in_immutables, library_private_types_in_public_api

import 'package:flutter/material.dart';
import 'secondScreen.dart';
void main() {
  runApp(MainApp());
}


class MainApp extends StatefulWidget {
  MainApp ({Key ? key}) : super(key: key);

  @override
  _MainAppState createState() => _MainAppState();

}

class _MainAppState extends State<MainApp> {
  int _currentstep = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        scaffoldBackgroundColor: Colors.grey,
        appBarTheme: AppBarTheme(
          color: Colors.grey[50]
        )
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text("Writing your first python code "),
        ),
        body: Center(
          child: Stepper(
            currentStep: _currentstep,
            onStepContinue: () => {
              setState(() {
                if(_currentstep < 100) {
                _currentstep ++;
              }
              })
            },
            onStepCancel: () => {
              setState(() {
                if (_currentstep > 0) {
                _currentstep --;
              }
              })
            },
            steps: [
              Step(
                title: Text('Step One'),
                content: Text('Open Chrome')
              ),
              Step(
                title: Text('Step Two'),
                content: Text('After you open chrome search for Vscode')
              ),
              Step(
                title: Text('Step Three'),
                content: Text('Open Vscode')
              ),
              Step(
                title: Text('Step Four'),
                content: Text('Click File')
              ),
              Step(
                title: Text('Step Five'),
                content: Text('Press New Text File then click select a language')
              ),
              Step(
                title: Text('Step Six'),
                content: Text('Select Python')
              ),
            ],
          ), 
        ),
        bottomNavigationBar: Padding(
          padding: EdgeInsets.all(8.0),
          child: ElevatedButton(
            onPressed: () => {
              Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondScreen()))
            },
            child: Text('Done'),
          ),
        ),
      ),
    );
  }
}
// ignore_for_file: library_private_types_in_public_api, prefer_const_constructors, use_key_in_widget_constructors

import 'package:flutter/material.dart';

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Screen'),
      ),
    );
  }
}

Error: ════════ Exception caught by gesture ═══════════════════════════════════════════

Navigator operation requested with a context that does not include a Navigator.

════════════════════════════════════════════════════════════════════════════════

I tried googling answer , i tried all kinds of buttons , I tried to change file name of the second screen , nothing worked , I don’t know what happened I mean I expected this ti be a piece of cake but unfortunately it isn’t I thought flutter is beginner friendly I mean it is I made like two apps so far but dk what happened

2

Answers


  1. Use a router package like
    go_router

    code sample is provided in the example section. Another good package is GetX.

    Login or Signup to reply.
  2. Do this, make the home widget as seperate widget just like Second Screen and pass it to MaterialApp. The prblm you are facing is because the context you are using for belong to Material App not the FirstScreen Or you can make Builder as parent widget of your home scaffold and pass it to Navigator. Distinguishing between context is very important. MaterialApp internally wrap everything with Navigator but as you passes MaterialApp app context to Naviagtor which is it’s parent it is throwing this error. Just make the first screen as different stateless or state full widget and pass it to MaterialApp. It will work fine.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search