Reputation: 35
Is there a way of defining a List in Dart with mixed types, while explicitly setting the types of each element?
I know there is a Tuple type that does this, but I was hoping there is a way of doing it with the built-in List type.
(Right now my way of doing so is using a List<dynamic>
or a List<Object>
)
Example of what I'm looking for:
final mixedList = <bool, String>[false, 'Hello'];
Upvotes: 0
Views: 156
Reputation: 71683
No.
That thing is a tuple, not a list (sequence of similarly typed values).
If you could create a <bool, String>[false, 'Hello']
list, then the type of list[0]
and list[1]
depends on the value of the index.
That breaks static typing, since it's a kind of dependent typing (the type of an expression depends on the value of another).
Upvotes: 1