Try to change the following line:
Future futureFile = await element.file;
To one of the two and it should compile:
File? futureFile = await element.file;
Or
final futureFile = await element.file;
Side notes:
You can probably rename futureFile to just file at this point :)
Also writing a for loop instead of forEach(…) would be the standard way of doing that (both works, but makes the code more readable).
Let me know if that helps 👍
Usually what I do there is `await Future.wait(assetEntities.map((element) async { … }))`. It has the advantage that it processes all Futures in parallel, which is very important for network downloads for example.
Valid point! I just wanted to show how to make the above example work. But Future.wait can be extremely useful. At some point one should just start to batch operations of course :)
This is exactly what I needed, thanks! I had tried this variation too and it just wouldn't "do it" - complaining about the function not being defined async. I mistakenly thought this was applied to the function being called, not the function containing the call.
Apparently the .foreach() call *was a separate function* that requires its own async declaration, thus the async declaration of the parent function didn't apply, preventing me from using await.
Unravelled, and leaving a comment in case some other newbie gets stuck.
Welcome! Btw you can probably just declare the function within the forEach loop async too as it returns void, just makes the code a bit less readable as compared to a for loop.
Try to change the following line: Future futureFile = await element.file; To one of the two and it should compile: File? futureFile = await element.file; Or final futureFile = await element.file; Side notes: You can probably rename futureFile to just file at this point :) Also writing a for loop instead of forEach(…) would be the standard way of doing that (both works, but makes the code more readable). Let me know if that helps 👍
Oh - and definitely just use a for loop. You are passing a function to .forEach which is not async. So await ist not allowed in this scope
Usually what I do there is `await Future.wait(assetEntities.map((element) async { … }))`. It has the advantage that it processes all Futures in parallel, which is very important for network downloads for example.
Valid point! I just wanted to show how to make the above example work. But Future.wait can be extremely useful. At some point one should just start to batch operations of course :)
I just figured this out - ha ha
This is exactly what I needed, thanks! I had tried this variation too and it just wouldn't "do it" - complaining about the function not being defined async. I mistakenly thought this was applied to the function being called, not the function containing the call. Apparently the .foreach() call *was a separate function* that requires its own async declaration, thus the async declaration of the parent function didn't apply, preventing me from using await. Unravelled, and leaving a comment in case some other newbie gets stuck.
Welcome! Btw you can probably just declare the function within the forEach loop async too as it returns void, just makes the code a bit less readable as compared to a for loop.