Reputation: 8407
In flutter we can pass a stateless widget that returns a MaterialApp
instance to the runApp()
function like this:
void main()=>runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
...
);
}
}
or we can pass the instance of MaterialApp
directly to the runApp()
function like so:
void main()=>runApp(
new MaterialApp(
...
);
);
What is the difference between these to ways? Thanks.
Upvotes: 9
Views: 11043
Reputation: 657248
For hot-reload to be able to keep the state, it applies code changes and re-runs build()
so that the view is updated. If the whole app would be restarted, the previous state would be lost. This is not desired. If you want this use hot restart instead.
This also means that changes to code that is only executed when the whole app is restarted, will not be applied to the current state.
For more details about hot-reload and limitations see https://flutter.io/docs/development/tools/hot-reload
To add custom behavior on hot-reload the method State<T>.reassemble
can be overridden https://docs.flutter.io/flutter/widgets/State/reassemble.html
Upvotes: 2
Reputation: 276997
There's no difference in visual behavior. What changes is how hot reload behaves.
For example if you used runApp(MaterialApp())
, changing from
runApp(MaterialApp(title: 'Foo'))
to
runApp(MaterialApp(title: 'Bar'))
then the hot reload wouldn't take changes into consideration.
While if you had the following class :
class MyApp {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Foo',
);
)
}
and used it like this :
runApp(MyApp())
then changing title
of MyApp
would be correctly hot reloaded.
Upvotes: 17
Reputation: 239841
In one case you have a class, which you can add more methods to, and use them. In one case you don't.
Upvotes: 1