-
List
6-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
Lecture1.5
-
Lecture1.6
-
Declare a List in Dart
To declare a List
the syntax is
List<data-type> <variableName>=List();
Here data type can be any like integer String
, Map
or user-defined(Class
) object like Student()
, if you don’t mention any data type by default dynamic
type is assigned that means any type of elements can be used in a List
. Mentioning data type make sure only a particular data type items will be added, if you are not sure about the value which is coming in List
then use dynamic
datatype.
void main() { List<String> movies=["Avengers","IronMan","Harry Potter-I","Harry Potter-II"]; print(movies); print(movies.runtimeType); List someData=["Avengers",2019,4.4,true]; print(someData); print(someData.runtimeType); }
OUTPUT
[Avengers, IronMan, Harry Potter-I, Harry Potter-II] List<String> [Avengers, 2019, 4.4, true] List<dynamic>
variableName
can be any name like studentList
, favoriteMovies
etc, and List()
create a new empty list i.e that is at the start there will be no items in list length will be 0. if you want to declare a variable with some predefined items then you can use it like this:
List<String> movies=["Avengers","IronMan","Harry Potter-I","Harry Potter-II"];
You can also create an empty list by
List<data-type> <variableName>=[];