0

I have a list of urls ending in .jpg, I am using this package: gallery_saver (https://pub.flutter-io.cn/packages/gallery_saver)

After saving the first image, the app crashes. If there's only one image in the list then looks fine. I guess there is an issue in the async?

int downloadedImages = 0;
this.selectedImages.forEach((imageUrl) async {
     await GallerySaver.saveImage(imageUrl,albumName: 'My App').then((value){
       downloadedImages++;
     });
});

I am not able to find any examples on how to save multiple images from the docs, any idea?

Please see the below:

I/flutter (20277): http://example.com/images/1638413558kuRlUfWDcH.jpg
I/flutter (20277): null
I/flutter (20277): http://example.com/images/1638413558eFHSBeRlF1.jpg
I/flutter (20277): null
I/flutter (20277): File size:47904
I/flutter (20277): /data/user/0/com.myapp.matrix/cache/1638413558kuRlUfWDcH.jpg
I/flutter (20277): File size:79960
I/flutter (20277): /data/user/0/com.myapp.matrix/cache/1638413558eFHSBeRlF1.jpg
E/AndroidRuntime(20277): FATAL EXCEPTION: main
E/AndroidRuntime(20277): Process: com.myapp.matrix, PID: 20277
E/AndroidRuntime(20277): java.lang.NullPointerException
E/AndroidRuntime(20277):        at carnegietechnologies.gallery_saver.GallerySaver.finishWithSuccess(GallerySaver.kt:79)
E/AndroidRuntime(20277):        at carnegietechnologies.gallery_saver.GallerySaver.access$finishWithSuccess(GallerySaver.kt:16)
E/AndroidRuntime(20277):        at carnegietechnologies.gallery_saver.GallerySaver$saveMediaFile$1.invokeSuspend(GallerySaver.kt:74)
E/AndroidRuntime(20277):        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/AndroidRuntime(20277):        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
E/AndroidRuntime(20277):        at android.os.Handler.handleCallback(Handler.java:938)
E/AndroidRuntime(20277):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(20277):        at android.os.Looper.loop(Looper.java:246)
E/AndroidRuntime(20277):        at android.app.ActivityThread.main(ActivityThread.java:8456)
E/AndroidRuntime(20277):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(20277):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
E/AndroidRuntime(20277):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
I/Process (20277): Sending signal. PID: 20277 SIG: 9
Lost connection to device.
Manas
  • 3,060
  • 4
  • 27
  • 55
  • *"After saving the first image, the app crashes"* - post the stacktrace then – pskink Dec 03 '21 at 07:49
  • @pskink just updated, thanks a lot in advance! – Manas Dec 03 '21 at 07:54
  • 1
    seems that package does not support multiple concurrent downloads - instead try `for (final imageUrl in selectedImages) { await GallerySaver.saveImage(imageUrl, ....` – pskink Dec 03 '21 at 08:01
  • @pskink that worked! Thank you! :) Just for my understanding, in the `forEach` loop I had in place, it's trying to use the same `GallerySaver` instance? whereas in your recommended `for` loop we are creating a new `GallerySaver` instance for every image. Is that corrrect? – Manas Dec 03 '21 at 08:08
  • your welcome, see https://stackoverflow.com/questions/63719374/how-to-wait-for-foreach-to-complete-with-asynchronous-callbacks/63719805#63719805 – pskink Dec 03 '21 at 08:11
  • Ah I see, that makes sense now, it has nothing to do with the `GallerySaver` instance, it's forEach sytax with async. thanks for sharing that! :) – Manas Dec 03 '21 at 08:20

0 Answers0