by @kodeazy

flutter How to initialize a list with same values?

Home » flutter » flutter How to initialize a list with same values?

In this blog we will discuss on how to initialize List in flutter with same values.

With the help of filled function we can achive adding same elements into the list.
Lets suppose we want to add 4 elements into a list with value as 3 in all of its positions.
Below is the syntax.

List<int>.filled(3, 4, growable: true);

Below is an example program of adding the elements into the list.

import 'dart:convert';
void main() {
  List<int> ele = List<int>.filled(3, 4, growable: true);
  print(ele);
}

Output

[4, 4, 4]