hasSpace method

Future<bool> hasSpace(
  1. int bytesNeeded
)

Implementation

Future<bool> hasSpace(int bytesNeeded) async{
  if(!await ready()) return false;
  try{
    var about = await api!.about.get($fields: "storageQuota");
    if(about.storageQuota == null || about.storageQuota?.limit == null){
      return false;
    }else if(about.storageQuota?.limit == null){ //Unlimited storage
      return true;
    }
    var limit = int.tryParse(about.storageQuota!.limit!);
    if(limit == null){
      return false;
    }
    var usage = int.tryParse(about.storageQuota!.usage!);
    if(usage == null){
      return false;
    }
    return limit - usage >= bytesNeeded;
  }catch(e, stack){
    if(e is PlatformException && e.code == "network_error"){
      return false;
    }
    if(kDebugMode){
      print("hasSpace:");
      print("${e.toString()}\n${stack.toString()}");
    }else if (onError != null){
      onError!(e, stack);
    }
    return false;
  }
}