skip to Main Content

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



2

Answers


  1. 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

    Login or Signup to reply.
  2. 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).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search