skip to Main Content

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());}

2

Answers


  1. Chosen as BEST ANSWER

    Based on discussions with @Cstark, the following steps is what made the error disappear:

    Attempted:

    Future _addPostMedia() {   debugLog('Adding post media');
    
      return Future.wait(
        _data.remainingCompressedMediaToUpload.map(
          (_uploadPostMediaItem as Function(File e)) as List<Future>));}

    Vscode suggested a cast as a quick fix and the following is the final code:

    Future _addPostMedia() {
      debugLog('Adding post media');
    
      return Future.wait(
        _data.remainingCompressedMediaToUpload.map(
          (_uploadPostMediaItem as Function(File e)) as Future<Object?> Function(File e))
      );
    }


  2. 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()
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search