why my setstate not working after time end?

Advertisements

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 :

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'));
  }
  
}

Leave a ReplyCancel reply