1
votes

I am trying to navigate to a new screen after the splash screen, when i try to navigate to new screen in the init method it shows an error,

E/flutter ( 5636): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.

E/flutter ( 5636): 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.

My code

import 'package:flutter/material.dart';
import 'package:igloled_app/home.dart';
import 'dart:async';

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();

}

class _SplashScreenState extends State<SplashScreen> {

  @override
  void initState() {
    super.initState();
      Future.delayed(Duration(seconds: 3),(){
        print('3 sec done');
        Navigator.push(context, MaterialPageRoute(builder: (context){
          return AppHeader();
        }));
      });


  }
  
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Stack(
          fit: StackFit.expand,
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                color: Colors.white70,
              ),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Expanded(flex: 2,
                    child: Container(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(20.0),
                        child: Container(
                          margin: EdgeInsets.all(10.0),
                           child: Image.asset('images/splash_logo.png')
                        ),
                      )
                    ],
                  ),
                    ),
                ),
                Expanded(flex: 1,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.green),),
                    Padding(padding: EdgeInsets.only(top: 20.0),
                    ),
                    Text('Make Your Life Brighter',
                      style: TextStyle(
                        color: Colors.lightGreen,
                        fontStyle: FontStyle.italic,
                        fontSize: 20.0,
                      ),
                    ),
                  ],
                ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}



  }

Can anyone please help me with this. Thank you in advance.

2
Please post full code of your class, where you are using initState - CopsOnRoad
@CopsOnRoad Updated my question. - Alvin John Niravukalayil
I just ran your code, and it looks fine, I am able to navigate to the second screen. - CopsOnRoad
Can you also post the code of your AppHeader class? - CopsOnRoad
@CopsOnRoad Thanks for your code, I need to add MaterialApp to my first screen too. - Alvin John Niravukalayil

2 Answers

1
votes

Here is a sample code of doing it right way.

void main() => runApp(MaterialApp(home: FirstScreen()));

class FirstScreen extends StatefulWidget {
  @override
  _FirstScreenState createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 3), () {
      print('3 sec done');
      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return SecondScreen();
      }));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(title: Text("First screen")));
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(title: Text("Second screen")));
  }
}
1
votes

your MaterialApp is not in your context !!

a slight fix to move the MaterialApp to the runApp method.

but for a notice please but all underground widgets that won't use the setState to another class or to the runApp method to have the best functionality of your code .