Home > OS >  Error Message: The return type 'void' isn't a 'Widget', as required by the
Error Message: The return type 'void' isn't a 'Widget', as required by the

Time:09-13

**Whenever I want to navigate to next page with specific time, the destination page isn,t found, even I imported the page. Instead of Navigation, if I print something, It works OK... Error line is highlighted in bold inside code, Kindly help in this regard.

Thanks in advance for your time and answer.... Whenever I want to navigate to next page with specific time, the destination page isn,t found, even I imported the page. Instead of Navigation, if I print something, It works OK... Error line is highlighted in bold inside code, Kindly help in this regard.

Thanks in advance for your time and answer **

import 'package:flutter/material.dart';
import 'dart:async';
import 'main.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "Flutter App",
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          color: Colors.deepOrangeAccent,
        ),
        primarySwatch: Colors.indigo,
      ),
      home: LoginScreen(),
    );
  }
}

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

  @override
  State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Timer(Duration(seconds: 5), () {
      // 5 seconds over, navigate to Page2.
      **Navigator.of(context).push(MaterialPageRoute(builder: (context)=>main()));****
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.indigo,
      body: Padding(
        //padding: const EdgeInsets.all(20.0),
        padding: const EdgeInsets.symmetric(horizontal: 20.0),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Center(
                child: Image.asset(
                  "assets/images/logo.png",
                  //fit: BoxFit.fitWidth,
                  height: 200,
                  width: 200,
                ),
              ),
              //CircularProgressIndicator(),
              SizedBox(
                height: 20,
              ),
              Text(
                "Fresh Food Restaurant",
                style: TextStyle(
                  color: Colors.deepOrangeAccent,
                  fontSize: 25,
                  fontWeight: FontWeight.w900,
                ),
              ),
              SizedBox(
                height: 150,
              ),
              // CircularProgressIndicator(
              //   backgroundColor: Colors.deepOrangeAccent,
              // ),
              // SizedBox(
              //   height: 10,
              // ),
              // Text(
              //   "Loading, Please wait",
              //   style: TextStyle(
              //     color: Colors.white,
              //   ),
              // ),
              SizedBox(
                height: 100,
              ),
              FloatingActionButton(
                backgroundColor: const Color(0xff03dac6),
                foregroundColor: Colors.black,
                onPressed: () {

                },
                child: Icon(Icons.arrow_forward),
              )
            ],
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

Main is not a widget and Navigator need Widget to push, like this:

Timer(Duration(seconds: 5), () {
  Navigator.of(context)
      .push(MaterialPageRoute(builder: (context) => MyApp()));
});

or

Timer(Duration(seconds: 5), () {
      Navigator.of(context)
          .push(MaterialPageRoute(builder: (context) => LoginScreen()));
    });

but not a method void.

CodePudding user response:

You are trying to navigate to a method:

Navigator.of(context).push(MaterialPageRoute(builder: (context)=>main()));

This method 'main()' is defined above:

void main() {
  runApp(const MyApp());
}

You have to pay attention to upper and lower case writing (If your widget is called Main()).

CodePudding user response:

Instead of using it in init state use it in Build Widget

      // 5 seconds over, navigate to Page2.
      **Navigator.of(context).push(MaterialPageRoute(builder: (context)=>main()));****
    });

Another MEthod is here

if(mounted){  Future.delayed(Duration(seconds:5)).then((_)=>
    Navigator.of(context).push(MaterialPageRoute(builder: (context)=>main()));
    );}
  }

  • Related