type'int'不是类型'列表<int>'类型铸造

  • 本文关键字:类型 int type 列表 flutter dart
  • 更新时间 :
  • 英文 :


如何解决这个问题:type 'int' is not a subtype of type 'List<int>' in type cast?现在我正试图做这样的事情:

ElevatedButton(
onPressed: () {
BasesService().SelectBaseAsync(
basesNames?[index]['id]);
},

此外,我试图转换为所需的类型,如basesNames?[index]['id'] as List<int>,但它也返回了相同的错误:type 'int' is not a subtype of type 'List<int>' in type cast

basesNames的打印-[{name: MyDb, id: 4}]

Future<bool> SelectBaseAsync(List<int> integers) async {

final hubConnection = HubConnectionBuilder()
.withUrl(
'http:mysecurelink'
)
.build();
await hubConnection.start();
bool select = false;
List<int>? saveInts;
if (hubConnection.state == HubConnectionState.Connected) {
await hubConnection
.invoke('SelectBaseAsync', args: [integers]).then((value) {
saveInts = integers;
select = value as bool;
});
}
hubConnection.onclose(({error}) {
throw Exception(error);
});
print(saveInts);
print(select);
return select;
}

正如您在print(basesNames)结果中看到的那样,basesNames[index]['id']返回的是int值,而不是list of int,因此您可以更改SelectBaseAsync构造函数以接受int而非List<int>,或者像这样传递值:

BasesService().SelectBaseAsync([basesNames?[index]['id']]);

最新更新