Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why my setstate not working after time end?

i’am complete beginner. I want a make some app. My first screen just image and after 5 seconds i want to go my login page. But my code not working.

import 'dart:async';

import 'package:flutter/material.dart';
import "./loginmenu.dart";
import "./loginanimation.dart";

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void timer() {
    Future.delayed(Duration(seconds: 5)).then((_) {
      setState(() {
        loginmenu();
      });
      timer();
    });
  }

  Widget build(BuildContext context) {
    return MaterialApp(home: loginanimation());
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to call timer function in initState method. I created small demonstration for you. After widget building for 5s the application is in animating state, if time expires we are in loaded state.

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
 bool  _isAnimating = true;
  
 @override
 void initState() {
   timer();
  }
  
 Future<void> timer() async {
    await Future.delayed(Duration(seconds: 5)).then((_) {
      setState(() {
        _isAnimating = false;
      });
    });
  }

  Widget build(BuildContext context) {
    return MaterialApp(home: _isAnimating ? Text('Animating') : Text('Loaded'));
  }
  
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading