What is the difference between runApp(new MyApp()) and runApp(new MaterialApp()) in flutter?

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.


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

Tags:

Flutter