Home » Android » android – Flutter error: Each child must be laid out exactly once. On building layout

android – Flutter error: Each child must be laid out exactly once. On building layout

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m starting to work with flutter. It seems pretty nice and easy, but there is not much resources when it comes to errors. Anyway, after long hours of thinking, checking and improvising, I started to search the net for answers and well, there was non. So I’d like to ask all of you, maybe someone will be able to explain to me why I get that error, what does it mean and how to get rid of it.

Oh, and before I will go in deep, I probably should mention I’m using flutter_bloc.

Ok so I’ve got code like this:

class Settings extends StatelessWidget {
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
          title: new Text("SomeApp",style: TextStyle(color: Colors.white)),
          automaticallyImplyLeading: false,
          backgroundColor: myBlue.shade50,

          actions: <Widget>[
            IconButton(
              icon: new Icon(FontAwesomeIcons.download,color:  Colors.white),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => DownloadView()),
                );
              },
            ),
            IconButton(
              icon: new Icon(FontAwesomeIcons.chevronCircleLeft,color:  Colors.white),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => MainWindow()),
                );
              },
            ),]
      ),
      body: Container(
        padding: EdgeInsets.symmetric(vertical: 16),
        alignment: Alignment.center,
        child: new BlocBuilder<SettingsBloc, SettingsState>(
          builder: (context, state) {
            if (state is SettingsNotLoaded) {
              return new Center(
                  child: Text(
                    'count1',
                    style: TextStyle(fontSize: 24.0,color: Colors.black),
                  )
              );
            } else if (state is SettingsLoaded) {  
              return new Center(
                  child: Text(
                    'count2',
                    style: TextStyle(fontSize: 24.0,color: Colors.black),
                  )
              );
            }
            else
              {
                return new Center(
                    child: Text(
                      'count3',
                      style: TextStyle(fontSize: 24.0,color: Colors.black),
                    )
                );
              }
          },
        ),

      ),
    );
  }

And when starting my app I see the appBar just the way I want, but I do not see any text in body (I should see either count1, count2 or count3), but instead I get the error: “Each child must be laid out exactly once.” referring to line 5 – return new Scaffold( .
Seriously, this is driving me crazy. Do anyone know anything?

Thanks in advance and best regards to everyone!

PS. Ofcourse looked for info on flutter_bloc page, on https://flutter.dev/docs/development/ui/layout/tutorial ,here and in google as well.

Edit 1
Some grammatical errors
Edit 2
Added edits info

How to&Answers:

Hi i just had the same problem i think its just a bug in flutter.Stop and then reinstall your app

Answer:

It definitely is a bug from flutter, I just faced the same issue, although the above solution is correct but it seems to fail sometimes.

Try the following command

flutter clean

then run again

flutter run

EDIT:
After being familiar with Flutter for about a week, now I do understand that making changes in code such as adding in a new dependency in pubspec.yaml requires stopping the app and Running again, I did notice that the GUI in the IDE (I use Android Studio) might have some issue, the above commands in the command line works just fine.

Happy Coding 🙂

Answer:

Try the following, it worked with me for Android devices.
1- Stop the simulation.
2- Go to Android virtual device manager.
3- Go to “Action” in the virtual device.
4- Click the down arrow to see the option.
5- Click “wipe data”
6- Run the simulation again.

Answer:

I had the same issue when i performed a hot reload, then i performed a hot restart and it went away. Looks like a Flutter bug.

Answer:

Recently I had the same error.

In my case the error was caused by nesting two Expanded Widgets into each other.

The error has gone by removing the parent Expanded Widget.