Dart Programming Arrays And Lists

08.20.20-What-is-Dart-and-how-is-it-used-1320x742.jpg In this article, we are going to show you how to create and use lists in Dart. With a simple definition, an array is an object used to a collection of values. This collection could consist of anything : numbers, strings, etc. Dart represents arrays in the form of Lists objects. Lets start with a simple example.

List<int> numbers = [1, 2, 3, 6, 7, 8, 8];
List<String> names = ['john', 'nevil', 'anna'];

In the example above we have two lists, one with integer values and the other with string values.

Adding elements to the list

void main() {
  List<int> numbers = [1, 2, 3, 6, 7, 8, 8];
  numbers.add(12); //adding single items to the list.
  numbers.addAll([12, 34, 55, 66, 66]); //adding multiple items to the list.
  print(numbers);
}

Combining two array list

void main(){
  List<String> boys=['nevil','travis','pascal'];
  List<String> girls=['hope','lucy','grace'];
  var names=new List.from(boys)..addAll(girls);
  print(names);
}

In this example above, we have defined two lists, boys and girls both with different values. We have also defined a new variable names which stores the results. Remember, we are calling the values from the boys lists using List.from() and using addAll() function we are taking all the values from girls and creating a new list under the variable names

Checking if value exists in list

void main(){
   List<String> names=['nevil','travis','pascal'];
   if(names.contains('nevil')){
           print('there is name');
     }else{
             print('there is no name');
             }
}

In this Example we have a list names , and we are using the contain() function to find if a value exists in the list.

That's all for this article, remember there's a lot you can add do with lists in dart removing values in a list, filtering, sorting etc. Thanks for reading, if you find it useful remember to give us a feedback or if you wish to correct or in need of assistance just reach out to us. keep learning..