skip to Main Content

i have included the Timer but my splash screen not get pushed to login screen. i have used .pushreplacement method to push the splash screen to home .i have done many things but i still get same issue that splash screen not get push to Mylogin(Class).

My main.dart file

import 'package:flutter/material.dart';
import 'package:loginuicolors/home.dart';
import 'package:loginuicolors/login.dart';
import 'package:loginuicolors/register.dart';
import 'package:loginuicolors/splash_screen.dart';
import 'splash_screen.dart';
void main()  {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    home: Splash_screen(),
    routes: {
      'register': (context) => MyRegister(),
      'login': (context) => MyLogin(),
    },
  ));
}

Splash_screen.dart file

import 'dart:async';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:flutter/material.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'login.dart';

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

  @override
  State<Splash_screen> createState() => _Splash_screenState();
}

class _Splash_screenState extends State<Splash_screen> {
  void initstate() {
    super.initState();

    Timer(Duration(seconds: 2), () {
      Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => MyLogin(),
          ));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
            image: AssetImage('assets/splash_screen.png'), fit: BoxFit.cover),
      ),
    );
  }
}

2

Answers


  1. In your Splash_screen, you are calling the initState incorrectly.
    The code provided contains initstate instead of initState.

    Flutter requires the use of specific method names like initState for lifecycle methods to work properly.

    These kinds of issues can be annoying and can be avoided by using linting practices, such packages are flutter_lints , very_good_analysis and you can find more linters by doing a quick search.

    Login or Signup to reply.
  2. It is due the spelling mistake initState is the right one.
    initstate is being treated as a normal function which can be called any time.

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