Search code examples
flutterdartchildrenscaffold

How to add multiple children in a body


I am new to Dart and I am currently stuck on how to add multiple children in a body. Right now I need to add 4 children in a body and I do not know what to do.

I have tried this:

This is my body:

body: Column(
    children: [
      LinkBox(), // Add the link box here
    Expanded(child: MyHomePage()),
    ],
  ),

I need to add the following children into my body:

  1. LinkBox()
  2. MyHomePage()
  3. ShoppingItem()
  4. ShoppingGallery()

I have tried to do this:

body: Column(
    children: [
      LinkBox(), // Add the link box here
    Expanded(child: MyHomePage()),
    Expanded(child: ShoppingItem()),
    Expanded(child: ShoppingGallery()),
    ],
  ),

Also tried to do this:

body: Column(
    children: <Widget>[
      LinkBox(), // Add the link box here
     MyHomePage(),
    ShoppingItem(),
     ShoppingGallery(),
    ],
  ),

And also this:

body: Column(
    children: <Widget>[
      LinkBox(), // Add the link box here
      Expanded(child: MyHomePage()),
    Expanded(child: ShoppingItem()),
    Expanded(child: ShoppingGallery()),
    ],
  ),

It resulted with a lot of problems and I could not run the code on DartPad.

I want it to look like this:

image 1

with this below:

image 2

These are the problems that dart pad resulted in:

1st problem
2nd problem
3rd problem


Solution

  • As in error message, your widgets ShoppingItem and ShoppingGallery have required parameters which you did not provide, those are name, priceRange and rating. You are unable to build Flutter/Dart app without required parameters. Please read https://dart.dev/language/classes to know more about how classes work in dart (Widgets are essentially dart classes)