0

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);
                  },
                ),
              ],
            ),
          ),
        );
      }
    }
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Can you include steps to reproduce the same issue you are facing ? And I think this is a [duplicate question](https://stackoverflow.com/q/66004956/10157127) – Md. Yeasin Sheikh Jan 03 '22 at 12:22
  • Does this answer your question? [setState() or markNeedsBuild called during build](https://stackoverflow.com/questions/47592301/setstate-or-markneedsbuild-called-during-build) – Md. Yeasin Sheikh Jan 03 '22 at 12:25
  • I have already checked your suggested solution but I don't know where I have to use your code, can you tell me where to add it? – Noor Fatima Jan 03 '22 at 12:32
  • Does your issue occur on build time or while tap event? – Md. Yeasin Sheikh Jan 03 '22 at 12:44
  • The issue occurs on build time. – Noor Fatima Jan 03 '22 at 13:45
  • Failed to reproduce issue from above snippet by removing external widgets. Can you provide code-snippet without external widgets/packages or including them on question? – Md. Yeasin Sheikh Jan 03 '22 at 14:05
  • @NoorFatima your code is working fine on my side. Are you sure problem is in the same file? – Diwyansh Jan 03 '22 at 14:06
  • Diwyansh I think so but I am sharing you the main.dart file code, please check it also void main() => runApp(FlashChat()); class FlashChat extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, initialRoute: WelcomeScreen.id, routes: { WelcomeScreen.id: (context) => WelcomeScreen(), LoginScreen.id: (context) => LoginScreen(), RegistrationScreen.id: (context) => RegistrationScreen(), ChatScreen.id: (context) => ChatScreen(), }, ); } } – Noor Fatima Jan 03 '22 at 14:23
  • https://ibb.co/j8S1y6Y this is the error screenshot. https://ibb.co/dbJD0bP It just appeared after running the code and after that login screen appears giving errors. – Noor Fatima Jan 03 '22 at 15:02

2 Answers2

0

Try WidgetsBinding.instance.addPostFrameCallback.

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
 setState(() {
  });
}
Benjith Kizhisseri
  • 2,311
  • 2
  • 13
  • 24
0

copy and paste the code

animation = ColorTween(begin: Colors.blueGrey, end: 
Colors.white).animate(controller);
    controller.forward();
    controller.addListener(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
         setState(() { });
         }
    });
Hamza Siddiqui
  • 675
  • 5
  • 12
  • this code is still not working. Firstly it gives an error to add a null check like this WidgetsBinding.instance!.addPostFrameCallback((timeStamp) { setState(() { }); }); after putting null check, the list of errors apear as : setState() or markNeedsBuild() called during build. 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4165 pos 12: '!_debugLocked': is not true. A RenderFlex overflowed by 199242 pixels on the bottom. 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4165 pos 12: '!_debugLocked': is not true. – Noor Fatima Jan 03 '22 at 13:55
  • try removing setstate or use Future.delayed(Duration(seconds:2),(){ setState(() { }); }); – Hamza Siddiqui Jan 03 '22 at 14:01
  • added but still same errors. – Noor Fatima Jan 03 '22 at 14:25