我正在嘗試創建一個函式,該函式利用 dart 中的串列的 shape 方法。我的問題是我無法在傳遞給函式的二維串列上呼叫 shape。奇怪的是 shape 在函式內創建的串列上作業正常。我不明白這是為什么,因為他們似乎在所有方面都是平等的。這是我的代碼:
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]);
}
uj5u.com熱心網友回復:
該shape函式很可能是 typeList<List<int>>或它的某個超型別的靜態擴展方法。
image這里引數的靜態型別是dynamic. 擴展方法基于接收者運算式的靜態型別應用,并且dynamic不是 的子型別,List<List<int>>因此這些shape方法不適用。(該dynamic型別的特殊之處在于沒有擴展方法適用于它,因為它被認為具有所有成員本身。)
如果您將代碼更改為
void testFunc(List<List<int>> image) {
// ...
}
那么它可能會開始作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/327219.html
