Do’s and Don’ts when you use List in your DART CODE !!

Image Copyright to the respective owner
List<int> _items = [];

1. Add Elements:

  • .add(E value): This method adds a single element of type E (here int, as we have List<int>) at the end of the list.
  • .addAll(List<E> value): This method adds all elements present in the list value into another list on which addAll is used.
  • .insert(int index, E element): This method inserts an element of type E at the mentioned index into the list.
  • .insertAll(int index, List<E> value): This method inserts the list value at the mentioned index into the list on which insertAll is used.
for(int i=0; i<10; i++){
_items.add(i);
}

However, for loops should be restricted as much as possible! It takes a lot of resources.

_items = List.generate(10, (index) => index);
print(_items);
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2. Remove Element:

  • To remove a particular element, we can use .remove(E Value) This will remove the first occurrence of value in the List. Eg: _items.remove(2) This will remove the value 2 from the list _items.
  • To remove the last element from the list, we can use .removeLast() . Eg: _items.removeLast() . This will remove the last element (here value 9 ) from the list _items
  • To remove element at a particular index, we can use .removeAt(int index) . Eg: _items.removeAt(5) . This will remove the value of _items[5] .
  • Using for loop (don’t use):
for (int i = 0; i < _items.length; i++) {
if (_items[i] % 2 == 0)
_items.removeAt(i);
}
  • Using for-each loop (better than forbut still, we have one better solution for this):
_items.forEach((element) {
if (element % 2 == 0) _items.remove(element);
});
Concurrent modification during iteration: Instance(length:9) of '_GrowableList'.
  List<int> _temp = [];
_temp.addAll(_items);
_items.forEach((element) {
if (element % 2 == 0) _temp.remove(element);
});
_items.clear();
_items.addAll(_temp);
  • Using .removeWhere
    This is one of the most efficient methods to remove elements based on some condition!
_items.removeWhere((element) => element % 2 == 0);

3. Update Element:

_items = _items.map((e) => e % 2 == 0 ? e + 1 : e).toList();

Don’t forget to connect with me on:

Don’t stop, until you are breathing!💙
- Abhishek Doshi

--

--

--

Google Developer Expert — Dart, Flutter & Firebase 💙💛

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Image Compression Using Bit-plane Slicing — OpenCvSharp(Without Pre-defined Functions)

Optimizing Database Loads

Common C# exceptions and how to fix them

Building Cloud Apps with Civo and Docker — Part 1: Setting Up the Cluster

Tech Founders and Leaders, Stop Recruiting and Read This First

Introducing native support for OpenTelemetry in Jaeger

Relationship between the Cloud Custodian Lambda Function and the Cloudwatch Event Rule (Part 2)

CI/CD pipeline for .NET

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Doshi

Abhishek Doshi

Google Developer Expert — Dart, Flutter & Firebase 💙💛

More from Medium

Forget the pain of Assets — Flutter💙

Why Flutter is the future of mobile app development

Why Flutter is the future of mobile app development

Difference between const and final in dart

Flutter checkbox without setState()