main and runApp
- Flutter course
- Beginner
- Lesson 8/99
- 4 min
Learn how main connects MaterialApp via runApp.
main and runApp
The entry point of every Flutter app is the main function. Inside main you call runApp, which attaches the root widget to the screen. The root widget is usually MaterialApp or CupertinoApp; they provide theme, navigation and Material/Cupertino components.
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Text('Merhaba'),
),
);
}Role of MaterialApp
MaterialApp gives the app a Material Design theme, title bar, navigation infrastructure and base theme settings. The home parameter sets the app's opening screen. Without MaterialApp, widgets like Text lack default styles.
MaterialApp(
title: 'İlk Uygulama',
home: Scaffold(
appBar: AppBar(title: Text('Başlık')),
body: Center(child: Text('Merhaba')),
),
)Practice Start with MaterialApp
Inside main, first write a print('basladi') call, then call runApp with a MaterialApp whose home is Text('Selam').
Starter code
import 'package:flutter/material.dart';
void main() {
// önce bir başlangıç mesajı yazdır
// sonra runApp ile bir MaterialApp çağır, home parametresi Text olsun
}Sample solution
import 'package:flutter/material.dart';
void main() {
print('basladi');
runApp(MaterialApp(home: Text('Selam')));
}Expected output
basladi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which widget is usually given to runApp as the root?
- ListView
- Column
- Scaffold
- MaterialApp Correct answer
Explanation: The root widget is usually MaterialApp; Scaffold and Column sit beneath it.
2 Bind the root widget
runApp(
____(
home: Text('Selam'),
),
);
runApp(
MaterialApp(
home: Text('Selam'),
),
);
Explanation: The root widget is usually MaterialApp; it provides theme and navigation.
3 main, runApp, MaterialApp
| Term | Meaning |
|---|---|
main |
App entry point |
runApp |
Attaches the root widget to the screen |
MaterialApp |
Provides Material theme and navigation |
home |
Sets the opening screen |