skip to Main Content

I an new to flutter and learning about rows and column classes. I am trying to add a container to the column, but it’s throwing errors.

void main(){
  runApp(
    const Myapp()
  );
}

class Myapp extends StatelessWidget {
  const Myapp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.teal,
        body: SafeArea(
          child: Column(
            children: <Widget>[
              Text('Text 1'),
              Container(
                height: 100,
                width: 100,
                color: Colors.blue,
                child: Text('Text 2'),
              )
            ],
          )
        ),
      ),
    );
  }
}

I am getting errors when I add container as a child in column.
Here are the errors :
enter image description here

I am following a tutorial and doing the same still getting error.
Thank you for your time.

2

Answers


  1. You cant use const in there. Column cant be constant. So you have to remove const. Just:

    void main(){
      runApp(
        const Myapp()
      );
    }
    
    class Myapp extends StatelessWidget {
      const Myapp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(/// Here we remove const
          home: Scaffold(
            backgroundColor: Colors.teal,
            body: SafeArea(
              child: Column(
                children: <Widget>[
                  Text('Text 1'),
                  Container(
                    height: 100,
                    width: 100,
                    color: Colors.blue,
                    child: Text('Text 2'),
                  )
                ],
              )
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. Simply you should remove

    const

    keyword, and will work fine.

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