skip to Main Content

So, I am trying to put multiple objects into a body for which I used Dart’s Stack function. While doing that I am getting this error. I am new to Dart and I am still learning about it’s data types.

I have two questions mainly –

  1. What are the data types that I can put inside the Stack function?
  2. Is this an efficient way in Dart to add multiple objects in a body:?

For your reference I am attaching a code below –

body: Stack(
      children: [Align(
        alignment: Alignment(0, 0),
        child: Text('Test?',
        style: TextStyle(
          fontSize: 28.0,
          color: Colors.black87,
          letterSpacing: 1.0,
          fontFamily: 'Quicksand',
          ),//TextStle
        ),//Text
      ),//Align

        Column(
          children: <widget>[
            Container(
              padding: EdgeInsets.all(20),
              color: Colors.cyan,
              child: Text('What's up')
            ),//Container
          ],//<Widget>
        ),//Column
      ]
  ),



Error: 'widget' isn't a type.
          children: <widget>[
                     ^^^^^^

4

Answers


  1. Try it without the <widget>

    Column(
              children:[
                Container(
                  padding: EdgeInsets.all(20),
                  color: Colors.cyan,
                  child: Text('What's up')
                ),
              ],
            ),
    

    more information about stack function – https://api.flutter.dev/flutter/widgets/Stack-class.html

    Login or Signup to reply.
  2. Try below code just remove in column refer Stack widget here

     Stack(children: [
                  Align(
                    alignment: Alignment(0, 0),
                    child: Text(
                      'Test?',
                      style: TextStyle(
                        fontSize: 28.0,
                        color: Colors.black87,
                        letterSpacing: 1.0,
                        fontFamily: 'Quicksand',
                      ), //TextStle
                    ), //Text
                  ), //Align
    
                  Column(
                    children: [
                      Container(
                          padding: EdgeInsets.all(20),
                          color: Colors.cyan,
                          child: Text('What's up')), //Container
                    ], //<Widget>
                  ), //Column
                ]),
    

    Result of your screen:

    enter image description here

    Login or Signup to reply.
    1. Stack is not a function, it creates a stack layout of widgets[means bind with a lot of children].
    2. The list literal type ‘List’ isn’t of expected type ‘List’. The list’s type can be changed with an explicit generic type argument or by changing the element types.

    you need not define it as a widget. Remove <widget>

    Stack(children: [
              Align(
                alignment: Alignment(0, 0),
                child: Text(
                  'Test?',
                  style: TextStyle(
                    fontSize: 28.0,
                    color: Colors.black87,
                    letterSpacing: 1.0,
                    fontFamily: 'Quicksand',
                  ), //TextStle
                ), //Text
              ), //Align
    
              Column(
                children: [
                  Container(
                      padding: EdgeInsets.all(20),
                      color: Colors.cyan,
                      child: Text('What's up')), //Container
                ], //<Widget>
              ), //Column
            ])
    
    Login or Signup to reply.
    1. in Stack you should put widgets. or a function that returns a widget. the data type here is a widget.

    2. for the body you have to add the widget to or a function that returns a widget. so the efficient way to add multiple items to the body is to use widgets that are able to take multiple children such as stack, column

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search