Introduction
Sometimes, we may wish to update a list item in Flutter/Dart. In this article, we'll look at how to do it.
Posts related to lists in Flutter/Dart:
- Filter items in a List in Flutter/Dart
- Sort a List in Flutter/Dart
- FlatMap a list of lists in Flutter/Dart
- Reverse a List in Flutter/Dart
- Iterate over List in Flutter/Dart
- Find elements in List in Flutter/Dart
- Remove items from a List in Flutter/Dart
- Access items from List in Flutter/Dart
- Create a list in Flutter/Dart
- Combine Lists in Flutter/Dart
How to update a list item in Flutter
There are several ways to update a list item/items in Flutter/Dart.
Replace item in a list using the []
operator
var list = ['zero', 1, 2, 3];
list[0] = 0;
list[3] = 'three';
print(list);
The output:
[0, 1, 2, 'three']
Replace items in a list using replaceRange()
method
Replace items from index 2 to 3:
var list = ['zero', 1, 2, 3];
list.replaceRange(2, 4, ['other 2', 'other 3']);
print(list);
The output:
[zero, 1, 'other 2', 'other 3]
Conclusion
In this post, we showcased two methods to replace/update an item in a list in Flutter/Dart.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}