How to update a list item in Flutter

October 16, 2022 No comments fluter list dart update item

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:

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.

{{ message }}

{{ 'Comments are closed.' | trans }}