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)
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.
List<string> myList = new List<string>() { "one", "two", "three", };
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.
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.
Yes:
List<int> options = [1, 2, 5, 9];
I'd recommend reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With