using the new keyword in flutter [duplicate]
Recently started following the flutter udacity course and while experimenting with creating basic apps I came across something that was unclear to me. When adding widgets, I have noted that doing both new Widget()
and Widget()
[where Widget is any widget being added to the tree] give the same result. Is there a specific time when you should use new Widget()
and a time when you should omit the new
keyword?
For example:
return MaterialApp(
debugShowCheckedModeBanner: false,
home: new Scaffold(
appBar: new AppBar(
title: Text('My app name')
),
)
Text('My app name')
works, but new Text('My app name')
also works. Any chance I could get some pointers and guidelines on best practices with this?
new
was made optional beginning with Dart 2.0, this is why some examples or tutorial still use new
and newer or updated ones don't.
You can just always omit it.
const
can be omitted when the context requires const