How do I properly catch errors when doing CRUD operations on firebase?
I turned off the wifi and I was expecting to catch an error snack but all I see is the loader spinning forever and ever. If I turn the wifi backon, it eventually uploads the files
_uploadImages(context) async {
setState(() {
_isLoading = true;
});
try {
List filesList = [];
var imgCount = await _getImageCountPerJob();
for (var photo in _imageFileList!) {
final destination = 'job-images/$jobId/img/img_$imgCount';
imgCount ;
final ref = FirebaseStorage.instance.ref(destination);
var file = File(photo.path);
await ref.putFile(file);
var imageUrl = await ref.getDownloadURL();
filesList.add(imageUrl);
}
_cloudFunctions
.updateJobColumn(
documentId: jobId,
fieldNameColumn: jobImagesListColumnt,
fieldNameColumnValue: filesList,
)
.then((value) {
_displaySnackBarMessage("Success uploading images", context);
setState(() {
_imageFileList = [];
});
});
} catch (err) {
log('error');
_displaySnackBarMessage("Error uploading images", context);
setState(() {
_isLoading = false;
});
}
setState(() {
_isLoading = false;
});
}
final job = FirebaseFirestore.instance.collection('job');
Future<void> updateJobApplicationColumn({
required String documentId,
required String fieldNameColumn,
required fieldNameColumnValue,
}) async {
try {
await jobApplication.doc(documentId).update({
fieldNameColumn: fieldNameColumnValue,
});
} catch (e) {
throw CouldNotUpdateJobException();
}
}
CodePudding user response:
You can use connectivity_plus package by flutter community for that. listen to connectivity changes and manage your exceptions accordingly.
https://pub.dev/packages/connectivity_plus
CodePudding user response:
Firebase not throw error about connection because it was made to work offline. No need to load spinner, just let user do other things. Data later will be saved on server when user get connection back. Except storage Firebase offline support works in Firestore and RealtimeDatabase too. Create CRUD operation with documents they will instantly update data on user device and push changes to database if you won't have connection it will do it later even on next day (Mean push to server user will see instant change on his device).
CodePudding user response:
If you turn off the wifi your machine won't be able to make any requests, as it needs the internet to connect to the api. From this snippet-
catch (err) {
log('error');
_displaySnackBarMessage("Error uploading images", context);
setState(() {
_isLoading = false;
});
You should be able to see any errors in your debug console from the log('error')
line, you could also change it to debugPrint(err)
. What IDE are you using?