Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initialize a List on one line in Dart? (it's called a collection initializer in c#)

Tags:

dart

Is it possible to initialize a list on one line in Dart? Something like the following...

List<int> options = new List<int>{ 1,2,5,9 }; 

(this is possible in c# and is called a collection initializer)

like image 436
atreeon Avatar asked Apr 04 '18 12:04

atreeon


People also ask

How do you initialize a list in darts?

initialize list in simple way using operator [] . create and fill a list with specified value using filled() constructor. create a list containing all specified itemsusing from() constructor. create a 'const' list using unmodifiable() constructor.

How do you initialize a list in C#?

List<string> myList = new List<string>() { "one", "two", "three", };

What is initialization in Dart?

Dart has a unique syntax to initialize each field in a constructor. You initialize the field in the Initializer list, which is placed before a constructor body. It has the following syntax: You put a colon ( : ) before a constructor body. You assign each instance variable after the colon.

What is collection initializers in C#?

Collection initializers let you specify one or more element initializers when you initialize a collection type that implements IEnumerable and has Add with the appropriate signature as an instance method or an extension method. The element initializers can be a simple value, an expression, or an object initializer.


1 Answers

Yes:

List<int> options = [1, 2, 5, 9]; 

I'd recommend reading:

  • https://api.dartlang.org/stable/1.24.3/dart-core/List-class.html
like image 191
Darshan Rivka Whittle Avatar answered Sep 18 '22 19:09

Darshan Rivka Whittle