Home > Net >  1-setState() or markNeedsBuild() called during build. 2-Failed assertion: line 4165 pos 12: '!_
1-setState() or markNeedsBuild() called during build. 2-Failed assertion: line 4165 pos 12: '!_

Time:01-04

Login screen, registration screen, etc. all screens are displaying but welcome screen is not showing:

Errors:

1-setState() or markNeedsBuild() called during build.
2-Failed assertion: line 4165 pos 12: '!_debugLocked': is not true.
3-'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4165 pos 12: '!_debugLocked': is not true.

import 'package:flutter/material.dart';
import 'login_screen.dart';
import 'registration_screen.dart';
import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flash_chat/components/rounded_button.dart';
    

class WelcomeScreen extends StatefulWidget {
  static const String id = 'welcome_screen';
  @override
  _WelcomeScreenState createState() => _WelcomeScreenState();
}

class _WelcomeScreenState extends State<WelcomeScreen> with SingleTickerProviderStateMixin{
    
      late AnimationController controller;
      late Animation animation;
    
      @override
      void initState() {
        super.initState();
        controller = AnimationController(
          duration: Duration(seconds: 1),
          vsync: this,
        );
        animation = ColorTween(begin: Colors.blueGrey, end: Colors.white).animate(controller);
        controller.forward();
        controller.addListener(() {
          setState(() {});
        });
      }
    
      @override
      void dispose() {
        controller.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: animation.value,
          body: Padding(
            padding: EdgeInsets.symmetric(horizontal: 24.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Hero(
                      tag: 'logo',
                      child: Container(
                        child: Image.asset('images/logo.png'),
                        height: 60.0,
                      ),
                    ),
                    TypewriterAnimatedTextKit(
                      text: ['Chat'],
                      textStyle: TextStyle(
                        fontSize: 45.0,
                        fontWeight: FontWeight.w900,
                        color: Colors.black54,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 48.0,
                ),
                RoundedButton(
                  title: 'Log In',
                  colour: Colors.lightBlueAccent,
                  onPressed: () {
                    Navigator.pushNamed(context, LoginScreen.id);
                  },
                ),
                RoundedButton(
                  title: 'Register',
                  colour: Colors.blueAccent,
                  onPressed: () {
                    Navigator.pushNamed(context, RegistrationScreen.id);
                  },
                ),
              ],
            ),
          ),
        );
      }
    }

CodePudding user response:

Try WidgetsBinding.instance.addPostFrameCallback.

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
 setState(() {
  });
}

CodePudding user response:

copy and paste the code

animation = ColorTween(begin: Colors.blueGrey, end: 
Colors.white).animate(controller);
    controller.forward();
    controller.addListener(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
         setState(() { });
         }
    });
  • Related