I am trying to create a function that utilizes the shape method for lists in dart. My problem is that I cannot call shape on a 2d list that is passed into the function. The strange thing is that shape works fine on a list that was created within the function. I do not understand why that is because they appear to be equal in all ways. Here is my code:
void main() {
List<List<int>> image = [
[0, 10, 10, 0],
[20, 30, 30, 20],
[10, 20, 20, 10],
[0, 5, 5, 0]
];
testFunc(image);
}
void testFunc(image) {
List<List<int>> list = [
[0, 10, 10, 0],
[20, 30, 30, 20],
[10, 20, 20, 10],
[0, 5, 5, 0]
];
// works. output: 4
print(list.shape[0]);
// does not work. output: NoSuchMethodError: Class 'List<List<int>>' has no instance getter 'shape'.
print(image.shape[0]);
}
CodePudding user response:
Most likely the shape
function is a static extension method on the type List<List<int>>
, or some supertype of that.
The static type of the image
parameter here is dynamic
.
Extension methods are applied based on the static type of the receiver expression, and dynamic
is not a subtype of List<List<int>>
so the shape
methods does not apply.
(The dynamic
type is special in that no extension methods apply to it, because it's considered as having all members itself.)
If you change the code to
void testFunc(List<List<int>> image) {
// ...
}
then it will likely start working.