I have my code written like this but it gives an error saying:
Error: A value of type 'List' can't be assigned to a variable of type 'Widget'.
Column(
children: [
Question(
questions[_questionIndex]['questionText'],
),
...(questions[_questionIndex]['answers'] as List<String>)
.map((answer) {
return Answer(_answerQuestion, answer);
}).toList()
],
)
In the Dart programming language, the dot dot ( .. ) operator is also interpreted as "cascade notation". It allows you to not repeat the same target if you want to call several methods on the same object.
You can use the "double dot" to call functions on objects and access properties. This "operator" is simply used to make your code cleaner and concise. It often saves you from having to create temporary variables.
In Dart, Spread Operator (…) and Null-aware Spread Operator (…?) are used for inserting multiple elements in a collection like Lists, Maps, etc. Syntaxes: Spread operator.
Dart 2.3 introduce Spread operator (…)
Reference link : https://medium.com/flutter-community/whats-new-in-dart-2-3-1a7050e2408d
var a = [0,1,2,3,4];
var b = [6,7,8,9];
var c = [...a,5,...b];
print(c); // prints: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Dart 2.3 comes with the spread operator (...)
and the null-aware spread operator (...?)
, which allows us to add multiple elements in Collections.
List<String> values1 = ['one', 'two', 'three'];
List<String> values2 = ['four', 'five', 'six'];
var output = [...values1,...values2];
print(output); // [one, two, three, four, five, six]
For Flutter, we can use this inside column
Column(
children: [
...values.map((value) {
return Text(value);
}),
],
),
Output:
var x = [20,30,40];
var list = [1,2,3,x];
// Using the spread operator, adds the items individually not as a list
var separatedList = [1,2,3,...x];
print(list); // length = 4
print(separatedList); // length = 6
Results
[1, 2, 3, [20, 30, 40]]
[1, 2, 3, 20, 30, 40]
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