Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using the new keyword in flutter [duplicate]

Tags:

flutter

dart

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?

like image 696
AJ254 Avatar asked Jun 03 '18 16:06

AJ254


People also ask

What does New keyword do in flutter?

If you're coming from many other object-oriented languages, you've probably seen the new keyword used to create new instances of a class. In Dart, this new keyword works the same way, but it isn't necessary. In Dart 2, you don't need to use new or const to create an object. The compiler will infer that for you.

What is of () in flutter?

In the Flutter SDK the . of methods are a kind of service locator function that take the framework BuildContext as an argument and return an internal API related to the named class but created by widgets higher up the widget tree.

How do you rebuild another widget in flutter?

You can now rebuild a widget even when Flutter fails to do so. Flutter uses setState() to reference the state object and identify any change to the state. This way, you can track these changes and rebuild your application. In case this fails, keys will play an important role in forcing a rebuild of any preserved state.

What is reusable card in flutter?

It has multiple sequentials screens that have the same components, like appbar, float button and a card to display contents of each screen.


1 Answers

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

like image 197
Günter Zöchbauer Avatar answered Oct 21 '22 18:10

Günter Zöchbauer