skip to Main Content

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

2

Answers


  1. 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)

    Login or Signup to reply.
  2. class ShoppingItem {
      
      String name;
      int priceRange;
      double rating;
      
      ShoppingItem({required this.name,required this.priceRange,required this.rating});
    }
    
      class ShoppingGallery {
      
      String name;
      int priceRange;
      double rating;
      
      ShoppingGallery({required this.name,required this.priceRange,required this.rating});
    }
    
    void main() {
      ShoppingItem _item1 = ShoppingItem(name:"Dog",priceRange: 500,rating: 4.5);
      ShoppingItem _item2 = ShoppingItem(name:"Cat",priceRange: 300,rating: 3.5);
      
      List<ShoppingItem> _shoppingItems = [_item1,_item2];  
      
      ShoppingGallery _galleryItem1 = ShoppingGallery(name:"Toy",priceRange: 100,rating: 5);
      ShoppingGallery _galleryItem2 = ShoppingGallery(name:"Sweets",priceRange: 200,rating: 2);
      
      List<ShoppingGallery> _galleryItems = [_galleryItem1,_galleryItem2];  
      
      print(_shoppingItems); // [Instance of 'ShoppingItem', Instance of 'ShoppingItem']
      print(_galleryItems); // [Instance of 'ShoppingGallery', Instance of 'ShoppingGallery']`enter code here`
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search