预期在可迭代的映射函数中找到)。

huangapple go评论69阅读模式
英文:

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(&#39;Adding post media&#39;);

return Future.wait(_data.remainingCompressedMediaToUpload
.map(_uploadPostMediaItem as Function(File e)) as Iterable&lt;Future&gt;
.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(&#39;Adding post media&#39;);

  return Future.wait(
    _data.remainingCompressedMediaToUpload.map(
      (_uploadPostMediaItem as Function(File e)) as Iterable&lt;Future&gt;
    ).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(&#39;Adding post media&#39;);

  return Future.wait(
    _data.remainingCompressedMediaToUpload.map(
      (_uploadPostMediaItem as Function(File e)) as List&lt;Future&gt;));}

<!-- 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(&#39;Adding post media&#39;);

  return Future.wait(
    _data.remainingCompressedMediaToUpload.map(
      (_uploadPostMediaItem as Function(File e)) as Future&lt;Object?&gt; Function(File e))
  );
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月26日 21:24:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定