I am trying to assign the length of a list to a double and it says that an instance member can’t be accessed in an initializer:
List numlist = [1,2,3,4,5];
double size = numlist.length.toDouble();
Is there a way to get this to work?
I am trying to assign the length of a list to a double and it says that an instance member can’t be accessed in an initializer:
List numlist = [1,2,3,4,5];
double size = numlist.length.toDouble();
Is there a way to get this to work?
2
Answers
use
late
to declare a variable that will be initialized at a later timein this case,
size
should be initialized afternumlist
Use
Getter
: Dynamically computes the size whenever it is accessed, ensuring it is always up-to-date with the current length of the list.For example: