Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

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.

like image 218
Toni Joe Avatar asked Jul 12 '18 19:07

Toni Joe


People also ask

What is the difference between main () and runApp () functions in Flutter?

The main() function came from Java-like languages so it's where all program started, without it, you can't write any program on Flutter even without UI. The runApp() function should return widget that would be attached to the screen as a root of the widget Tree that will be rendered.

What is the difference between WidgetsApp and MaterialApp?

What is the difference between WidgetsApp and MaterialApp ? WidgetsApp provides basic navigation. Together with the widgets library, it includes many of the foundational widgets that Flutter uses. MaterialApp and the corresponding material library is a layer built on top of WidgetsApp and the widgets library.

What is the difference between material and MaterialApp in Flutter?

MaterialApp is the starting point of your app, it tells Flutter that you are going to use Material components and follow material design in your app. Scaffold is used under MaterialApp , it gives you many basic functionalities, like AppBar , BottomNavigationBar , Drawer , FloatingActionButton etc.

What does MaterialApp do in Flutter?

MaterialApp class Null safety. An application that uses material design. A convenience widget that wraps a number of widgets that are commonly required for material design applications. It builds upon a WidgetsApp by adding material-design specific functionality, such as AnimatedTheme and GridPaper.


Video Answer


3 Answers

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.

like image 54
Rémi Rousselet Avatar answered Nov 12 '22 08:11

Rémi Rousselet


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

like image 22
Günter Zöchbauer Avatar answered Nov 12 '22 06:11

Günter Zöchbauer


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

like image 40
hobbs Avatar answered Nov 12 '22 06:11

hobbs