Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Lists, Maps, and Sets in Flutter | Dart Language Core for Flutter
Dart for Flutter Developers

bookLists, Maps, and Sets in Flutter

Dart offers three primary collection types: List, Map, and Set. Each serves a different purpose when managing data in Flutter UIs. A List is an ordered group of items, perfect for showing repeated widgets like a list of contacts or messages. A Map stores key-value pairs, making it suitable for configuration or when you need to look up values by a unique key. A Set holds unique items and is useful when you need to ensure there are no duplicates, such as a collection of selected tags. In Flutter, you often use these collections to build and configure widgets dynamically.

main.dart

main.dart

copy
123456789101112131415161718192021222324
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { final List<String> fruits = ['Apple', 'Banana', 'Orange']; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Fruit List')), body: ListView.builder( itemCount: fruits.length, itemBuilder: (context, index) { return ListTile( title: Text(fruits[index]), ); }, ), ), ); } }

You can use a Map in Flutter to configure widget properties. For example, a Map might hold theme colors, icon data, or other configuration options that widgets can read and apply.

main.dart

main.dart

copy
1234567891011121314151617181920212223242526272829
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { final Map<String, dynamic> buttonConfig = { 'text': 'Click Me', 'color': Colors.blue, 'icon': Icons.thumb_up, }; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: ElevatedButton.icon( icon: Icon(buttonConfig['icon']), label: Text(buttonConfig['text']), style: ElevatedButton.styleFrom( backgroundColor: buttonConfig['color'], ), onPressed: () {}, ), ), ), ); } }
Note
Note

Directly modifying collections inside a StatelessWidget can cause unexpected behavior because Flutter may not detect the changes and rebuild the UI properly. To manage changing data, use StatefulWidget or a state management solution to ensure the UI updates as expected.

question mark

Which Dart collection type is best for storing a list of unique categories that a user can select in a Flutter app?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookLists, Maps, and Sets in Flutter

Pyyhkäise näyttääksesi valikon

Dart offers three primary collection types: List, Map, and Set. Each serves a different purpose when managing data in Flutter UIs. A List is an ordered group of items, perfect for showing repeated widgets like a list of contacts or messages. A Map stores key-value pairs, making it suitable for configuration or when you need to look up values by a unique key. A Set holds unique items and is useful when you need to ensure there are no duplicates, such as a collection of selected tags. In Flutter, you often use these collections to build and configure widgets dynamically.

main.dart

main.dart

copy
123456789101112131415161718192021222324
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { final List<String> fruits = ['Apple', 'Banana', 'Orange']; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Fruit List')), body: ListView.builder( itemCount: fruits.length, itemBuilder: (context, index) { return ListTile( title: Text(fruits[index]), ); }, ), ), ); } }

You can use a Map in Flutter to configure widget properties. For example, a Map might hold theme colors, icon data, or other configuration options that widgets can read and apply.

main.dart

main.dart

copy
1234567891011121314151617181920212223242526272829
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { final Map<String, dynamic> buttonConfig = { 'text': 'Click Me', 'color': Colors.blue, 'icon': Icons.thumb_up, }; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: ElevatedButton.icon( icon: Icon(buttonConfig['icon']), label: Text(buttonConfig['text']), style: ElevatedButton.styleFrom( backgroundColor: buttonConfig['color'], ), onPressed: () {}, ), ), ), ); } }
Note
Note

Directly modifying collections inside a StatelessWidget can cause unexpected behavior because Flutter may not detect the changes and rebuild the UI properly. To manage changing data, use StatefulWidget or a state management solution to ensure the UI updates as expected.

question mark

Which Dart collection type is best for storing a list of unique categories that a user can select in a Flutter app?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 4
some-alt