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.
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 ? 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.
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.
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.
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
In one case you have a class, which you can add more methods to, and use them. In one case you don't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With