Superjay
Superjay

Reputation: 477

What does <Widget> means in Flutter code?

I am new to Flutter and studying with a book. And code in my book is saying like:

Column(
  children: <Widget>[
  // Other codes
  ]
)

But when I delete the <Widget> part, it works perfectly too. What does this <Widget> do and it is okay when I delete that part?

Upvotes: 0

Views: 197

Answers (2)

daddygames
daddygames

Reputation: 1928

The syntax is specifying that [] contains objects of type Widget.

You can define any list of any type with similar syntax.

For example:

List<int> i = <int>[1,2,3];

Or:

List<MyClass> myClasses = <MyClass>[];

This works without the <Widget> syntax as long as you only include Widgets inside the array. You will get an error if you try to include anything that is not a Widget.

So this will error:

return Column(children: [Text("Hello World!"), 24, "Broken Code")]);

Because types of int and String are not allowed.

This will work:

return Column(children: [Text("Hello, World!")]);

It works because the only item in the list is of type Widget.

Upvotes: 3

Sachin Mamoru
Sachin Mamoru

Reputation: 486

The column contains elements of type <Widget> It's like a type in a programming language.

It means that in the column you are going to display the widget set. Widget is the underlying building block of flutter applications.

You can delete any widget from that column, because what it only does is rendering an UI element.

Upvotes: 1

Related Questions