how to show error when trying to add/upload data to firebase in flutter when user is offline?

665

try this,

import 'dart:io';

Future _getImage(bool isCamera) async {
    var pickedImage;
    try {
      if (isCamera)
        pickedImage =
            await picker.getImage(source: ImageSource.camera, imageQuality: 20);
      else
        pickedImage = await picker.getImage(
            source: ImageSource.gallery, imageQuality: 20);

      if (pickedImage != null) {
      try {
        final result = await InternetAddress.lookup('google.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) 
        _uploadAndSetUrl(File(pickedImage.path));
       
    } on SocketException catch (_) {
    showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: Text("Connection Error !"),
            content: Text("Please connect to the internet."),
          )
      );
 }
        
      }
    } catch (e) {
      print('error here');
    }
  }
Share:
665
Ali Alqallaf
Author by

Ali Alqallaf

Updated on November 18, 2022

Comments

  • Ali Alqallaf
    Ali Alqallaf over 1 year

    I am basically trying to show an alert dialog when a user try to add a document or upload an image to firestore cloud/firebase storage. I tried that with try and catch blocks as well as .catchError() but it did not work, and it basically do this after getting an internet connection. what i want to do is to show an alert dialog and cancel the process even the user gets a connection back (he have to repeat the process of uploading again). here is a sample of my code:

    this is for picking and uploading an image:

     Future _getImage(bool isCamera) async {
        var pickedImage;
        try {
          if (isCamera)
            pickedImage =
                await picker.getImage(source: ImageSource.camera, imageQuality: 20);
          else
            pickedImage = await picker.getImage(
                source: ImageSource.gallery, imageQuality: 20);
    
          if (pickedImage != null) {
            _uploadAndSetUrl(File(pickedImage.path));
          }
        } catch (e) {
          print('error here');
        }
      }
    
      Future _uploadAndSetUrl(File image) async {
        try {
          _uploadingStatus(true);
          StorageUploadTask uploadTask = _bookImageRef.putFile(image);
          StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
          String downloadAddress = await _bookImageRef.getDownloadURL();
          _uploadingStatus(false);
          setState(() {
            _imageUrl = downloadAddress;
          });
          print(_imageUrl);
        } on PlatformException catch (e) {
          print('error: $e');
        } catch (e) {
          print('No image added');
        }
      }
    
    

    and there is another method for adding a document into firestore cloud. is there any way to do that?

    thank you in advance!

  • Niteesh
    Niteesh almost 4 years
    let me know if this helps