Back

[SOLVED] Problem with file error handling in Flutter x Appwrite

  • 0
  • Flutter
  • Storage
Ichigor
28 May, 2023, 18:30

I'm trying to handle the error if the file with the user id doesn't exist Appwrite store, then display the local image.

my code widget:

TypeScript
                          FutureBuilder<Uint8List>(
                              future: storage.getFilePreview(
                                bucketId: bucketId,
                                fileId: docID,
                              ),
                              builder: (context, snapshot) {
                                if (snapshot.hasData && snapshot.data != null) {
                                  final imageBytes = snapshot.data!;
                                  return CircleAvatar(
                                    radius: 70,
                                    backgroundColor: Colors.grey[200],
                                    backgroundImage: MemoryImage(imageBytes),
                                  );
                                } else if (snapshot.hasError) {
                                  print(
                                      'error get preview: ${snapshot.error}');
                                }
                                return CircularProgressIndicator();
                              },
                            ),

if the file the user has exists in the appwrite repository, then the image displays fine!

but if the user's files don't exist in the repository, then I get an error, and that makes sense.

I need to handle the error correctly if I get an error:

AppwriteException (AppwriteException: storage_file_not_found, The requested file could not be found. (404))

In this case, display the local image.

how do i do this?

TL;DR
The user is experiencing an error with file error handling in a Flutter x Appwrite project. They are using the `FutureBuilder` widget to fetch a file preview, and if the file exists, it is displayed. However, if the file does not exist, they receive an `AppwriteException` with a 404 error. The user wants to handle this error and display a local image instead. The solution to this problem is to modify the code in the `else if (snapshot.hasError)` block. Instead of just printing the error, the user should return a `CircleAvatar` widget with the local image as the `backgroundImage
Drake
28 May, 2023, 18:44

Isn't your hasError block executing?

Ichigor
28 May, 2023, 18:56

I still get an error and my application crashes. I tried uninstalling the application from the emulator, did flutter clean, flutter run. Didn't help

Ichigor
28 May, 2023, 18:58
TypeScript
                              future: storage.getFilePreview(
                                bucketId: bucketId,
                                fileId: docID,
                              ),
                              builder: (context, snapshot) {
                                if (snapshot.hasData && snapshot.data != null) {
                                  final imageBytes = snapshot.data!;
                                  return CircleAvatar(
                                    radius: 70,
                                    backgroundColor: Colors.grey[200],
                                    backgroundImage: MemoryImage(imageBytes),
                                  );
                                } else if (snapshot.hasError) {
                                  return CircleAvatar(
                                    radius: 70,
                                    backgroundColor: Colors.grey[200],
                                    backgroundImage: AssetImage(
                                        'assets/android12splash.png'),
                                  );
                                }
                                return CircularProgressIndicator();
                              },
                            ),```
Drake
28 May, 2023, 19:00

This is fine. This is because of your debugger

Ichigor
28 May, 2023, 19:03

Thank you so much!

I didn't know!

Drake
28 May, 2023, 19:06

[SOLVED] Problem with file error handling in Flutter x Appwrite

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more