Variables
Part of the Your First App section of Coddy's Flutter journey. Lesson 11 of 32.
A variable is a name for a value, so you can use the value by its name. Create one with its type, its name, an equals sign and the value:
String city = 'Paris';String is the type: this variable holds text. The line ends with a semicolon. From then on, city means 'Paris', and Text(city) shows Paris. There are no quotes around city here, because it is a name, not the word itself.
In a Flutter app you can create variables at the start of build, before the return.
Challenge
EasyAt the start of build there is a variable city. Change its value to 'Lisbon'.
The app shows the variable with Text(city), so the screen changes with it.
Try it yourself
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
String city = 'Paris';
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Text(city),
),
),
);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Your First App
Practice on your own: Online Flutter compiler