Lists
Part of the Your First App section of Coddy's Flutter journey. Lesson 14 of 32.
A list holds several values in order, inside square brackets:
List<String> colors = ['red', 'green', 'blue'];List<String> means a list of strings. Each value has a position, called its index, and counting starts at 0: colors[0] is 'red' and colors[2] is 'blue'.
colors.length is how many values the list holds, here 3.
Challenge
EasyUsing the friends list, make the two lines show:
- the first friend's name,
Ana 3 friends, with the number fromfriends.length
The Column around them puts one line under the other.
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) {
List<String> friends = ['Ana', 'Ben', 'Chen'];
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('First friend'),
Text('How many?'),
],
),
),
),
);
}
}
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