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

后端 未结 3 1644
春和景丽
春和景丽 2020-12-19 12:56

In flutter we can pass a stateless widget that returns a MaterialApp instance to the runApp() function like this:

void main()=>r         


        
相关标签:
3条回答
  • 2020-12-19 13:16

    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

    0 讨论(0)
  • 2020-12-19 13:17

    In one case you have a class, which you can add more methods to, and use them. In one case you don't.

    0 讨论(0)
  • 2020-12-19 13:23

    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.

    0 讨论(0)
提交回复
热议问题