Home > Mobile >  Nothing shown on screen when I try to run debug
Nothing shown on screen when I try to run debug

Time:10-05

I'm trying to create a splash screen but whenever I would run a "run without debugging" in vs code, nothing appears on screen but the default screen. I tried creating a navigator route, set the Home to different HomePage or SplashScreen but still there's no output in the screen. Is there a problem with my VSCode or my code is not enough? There's no indicator that there's an error in my code that's why I'm wondering why there's no output in the screen.

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



void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(primarySwatch: Colors.red),
          home: LoginPage(),
        );
      }
    }
    
    class SplashScreen extends StatefulWidget {
      const SplashScreen({Key? key}) : super(key: key);
    
      @override
      _SplashScreenState createState() => _SplashScreenState();
    }
    
    class _SplashScreenState extends State<SplashScreen> {
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        Timer(Duration(seconds: 3), () {
          Navigator.of(context)
              .pushReplacement(MaterialPageRoute(builder: (_) => HomePage()));
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.lightBlue,
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                //logo here 
                Image.asset(
                  'assets/images/idkbank_logo.png',
                  height: 120,
                ),
                SizedBox(
                  height: 20,
                ),
              ],
            ),
          ),
        );
      }
    }
    
    class HomePage extends StatefulWidget {
      const HomePage({Key? key}) : super(key: key);
    
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.lightBlue,
            title: Text("Welcome to IDKBank"),
          ),
          body: Container(child: Text("Home Page")),
        );
      }
    }

CodePudding user response:

First, you're missing ; in the end of second import. Second , when you try to use context in initState, try to use WidgetsBinding.instance.addPostFrameCallback In you case, it should be

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((_) {
      Timer(Duration(seconds: 3), () {
        Navigator.of(context)
            .pushReplacement(MaterialPageRoute(builder: (_) => HomePage()));
      });
    });
  }

In initState, build funtion is not executed yet, the context is kind of empty. It the reason why you need WidgetsBinding.instance.addPostFrameCallback here.

CodePudding user response:

Try Using This Solution

Please See this Solution It Will Help you the most.

  • Related