英文:
Expected to find ) for map function as iterable
问题
I'm getting an error in dart when I use the map function as iterable.
我在使用 map 函数作为可迭代对象时遇到错误。
Future _addPostMedia() {
debugLog('Adding post media');
return Future.wait(_data.remainingCompressedMediaToUpload
.map(_uploadPostMediaItem as Function(File e)) as Iterable<Future>
.toList());
}
Above I'm getting an error Expected to find ) just before the .toList()
这是原始代码,它不起作用,VSCode 建议一些修复:
上面的错误是在 .toList() 前面期望找到 )。
Future _compressPostMedia() {
debugLog('Compressing post media');
return Future.wait(
_data.remainingMediaToCompress.map(_compressPostMediaItem)
.toList());
}
英文:
I'm getting an error in dart when I use the map function as iterable.
Future _addPostMedia() {
debugLog('Adding post media');
return Future.wait(_data.remainingCompressedMediaToUpload
.map(_uploadPostMediaItem as Function(File e)) as Iterable<Future>
.toList());}
> Above I'm getting an error Expected to find ) just before the
> .toList()
This was the original code which wasn't working and VSCode recommend some fixes :
Future _compressPostMedia() {
debugLog('Compressing post media');
return Future.wait(
_data.remainingMediaToCompress.map(_compressPostMediaItem)
.toList());}
答案1
得分: 1
每个级别的转换需要包含在其括号集中,所以你遗漏了包裹 as Iterable 的括号。
Future _addPostMedia() {
debugLog('Adding post media');
return Future.wait(
_data.remainingCompressedMediaToUpload.map(
(_uploadPostMediaItem as Function(File e)) as Iterable<Future>
).toList()
);
}
英文:
Each level of casting needs to wrapped in its set of parenthesis, so you were missing a set wrapping the as Iterable.
Future _addPostMedia() {
debugLog('Adding post media');
return Future.wait(
_data.remainingCompressedMediaToUpload.map(
(_uploadPostMediaItem as Function(File e)) as Iterable<Future>
).toList()
);
}
答案2
得分: 1
基于与 @Cstark 的讨论,以下步骤是导致错误消失的原因:
尝试的代码:
Future _addPostMedia() {
debugLog('Adding post media');
return Future.wait(
_data.remainingCompressedMediaToUpload.map(
(_uploadPostMediaItem as Function(File e)) as List<Future>));
}
Vscode 建议进行类型转换以快速修复,以下是最终的代码:
Future _addPostMedia() {
debugLog('Adding post media');
return Future.wait(
_data.remainingCompressedMediaToUpload.map(
(_uploadPostMediaItem as Function(File e)) as Future<Object?> Function(File e))
);
}
英文:
Based on discussions with @Cstark, the following steps is what made the error disappear:
Attempted:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
Future _addPostMedia() { debugLog('Adding post media');
return Future.wait(
_data.remainingCompressedMediaToUpload.map(
(_uploadPostMediaItem as Function(File e)) as List<Future>));}
<!-- end snippet -->
Vscode suggested a cast as a quick fix and the following is the final code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
Future _addPostMedia() {
debugLog('Adding post media');
return Future.wait(
_data.remainingCompressedMediaToUpload.map(
(_uploadPostMediaItem as Function(File e)) as Future<Object?> Function(File e))
);
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论