How to Create or Use Array List in Dart/Flutter

In this post, we are going to show you the array List basis of Dart/Flutter. In Dart, the array is called List. In this article, you will learn example-wise how to create and use Array in Dart or Flutter.

List<String> countries = ["Canada", "Brazil", "USA", "Japan", "China","UK", "Uganda", "Uruguay"];
//string array

List<int> numbers = [1, 5, 10, 44, 11, 33];
//integer array

class Student{
   String name;
   int rollno;

   Student({required this.name, required this.rollno});
}
List<Student> students = [
   Student(name: "John", rollno: 23),
   Student(name: "Max", rollno: 10)
];

List<String> citylist = [];

List<String> citylist = [];

citylist.add("New York"); //single
citylist.addAll(["Moscow", "London", "Sydney"]); //multiple at once

print(citylist); //[New York, Moscow, London, Sydney]

List<String> countries = ["Canada", "Brazil", "USA", "Japan", "China","UK", "Uganda", "Uruguay"];

countries.removeWhere((str){
    return str == "Brazil";
}); //go through the loop and match content to delete from list
print(countries); //[Canada, USA, Japan, China, UK, Uganda, Uruguay]

Detailed Article: How to Add (Push) and Remove Item from List Array with Dart

List<String> asia = ["Nepal", "India", "Pakistan", "China", "Japan"];
List<String> europe = ["Germany", "France", "United Kingdom", "Spain"];

var list = new List.from(asia)..addAll(europe);
print(list);
//Output: [Nepal, India, Pakistan, China, Japan, Germany, France, United Kingdom, Spain]

See detailed article: How to Combine Two or More List Array in Dart/Flutter

List<String> countries = ["Canada", "Brazil", "USA", "Japan", "China","UK", "Uganda", "Uruguay"];

List<String> filter = [];
filter.addAll(countries);

filter.retainWhere((countryone){
    return countryone.toLowerCase().contains("U".toLowerCase());
    //you can add another filter conditions too
});
print(filter); //list of countries which contains 'U' on name
//output:  [USA, UK, Uganda, Uruguay]

Detailed Article: How to Filter List Array in Dart/Flutter

List:

List<String> countries = [
    "Nepal", 
    "India", 
    "Pakistan",
    "Bangladesh",
    "USA",
    "Canada",
    "China",
    "Russia",
];

[Ascending Order]

countries.sort((a, b){ //sorting in ascending order
    return a.compareTo(b);
});
print(countries);
//output: [Bangladesh, Canada, China, India, Nepal, Pakistan, Russia, USA]

[Descending Order]

countries.sort((a, b){ //sorting in descending order
    return b.compareTo(a);
});
print(countries);
//output: [USA, Russia, Pakistan, Nepal, India, China, Canada, Bangladesh]

List<String> countries = [
    "Nepal", 
    "Nepal", 
    "USA",
    "Canada",
    "Canada",
    "China",
    "Russia",
];

var seen = Set<String>();
List<String> uniquelist = countries.where((country) => seen.add(country)).toList();
print(uniquelist);
//output:  [Nepal, USA, Canada, China, Russia]

List<String> countries = ["USA", "United Kingdom","China", "Russia", "Brazil"];
countries.shuffle();
print(countries); 
// [United Kingdom, Brazil, Russia, China, USA]
//the output will be in different order every time this code is executed.

Detailed Article: How to Shuffle Array List in Dart/Flutter

List<String> countries = ["Canada", "Brazil", "USA", "Japan", "China","UK", "Uganda", "Uruguay"];

int x = 1;
countries.map((countryone){
   print("$x. $countryone");
   x++;
}).toList();

/*Output:
I/flutter ( 4268): 1. Canada
I/flutter ( 4268): 2. Brazil
I/flutter ( 4268): 3. USA
I/flutter ( 4268): 4. Japan
I/flutter ( 4268): 5. China
I/flutter ( 4268): 6. UK
I/flutter ( 4268): 7. Uganda
I/flutter ( 4268): 8. Uruguay
*/ 

In this way, you can work with an array list in Dart/Flutter.

No any Comments on this Article


Please Wait...