Created
June 6, 2023 08:31
-
-
Save mbaka-bilal/67c5adc9274a87e2ce327702fd22064b to your computer and use it in GitHub Desktop.
Help!!! (Failure not getting caught)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void createInfoAlert({ | |
List<String>? videos, | |
String? audio, | |
List<String>? images, | |
required int alertCategoryId, | |
required String headline, | |
required String body, | |
required String location, | |
double? longitude, | |
double? latitude, | |
}) async { | |
updateStatus = RequestStatus.loading; | |
// await Future.delayed(const Duration(seconds: 5),() { | |
// _error = "Could not create info alert"; | |
// updateStatus = RequestStatus.success; | |
// }); | |
List<String> videoDownloadUrls = []; | |
List<String> imageDownloadUrls = []; | |
List<String> audioDownloadUrls = []; | |
int totalFilesSize = 0; | |
bool hasException = false; | |
int totalUploaded = 0; | |
int uploadedCount = 0; | |
List<String> bigList = []; | |
if (videos != null && videos.isNotEmpty) { | |
bigList.addAll(videos); | |
} | |
if (images != null && images.isNotEmpty) { | |
bigList.addAll(images); | |
} | |
if (audio != null && audio != "") { | |
bigList.add(audio); | |
} | |
for (String path in bigList) { | |
/** Get total size to be uploaded **/ | |
final size = await File(path).length(); | |
totalFilesSize = totalFilesSize + size; | |
} | |
// print("-----------------big list size is ${bigList.length}---------------"); | |
/*** Begin loop ***/ | |
try { | |
for (String path in bigList) { | |
final completer = Completer<void>(); //So the loop can run one by one | |
if (hasException) { | |
print("----------------has exception ----------"); | |
completer.completeError(Exception()); | |
break; | |
} | |
// print("path is $path"); | |
/** Upload to firebase, it's different depending on the media type | |
And get the download url for each file. **/ | |
String remotePath; | |
if (path.endsWith("mp4") || | |
path.endsWith("3gp") || | |
path.endsWith("avi")) { | |
remotePath = 'alert/videos'; | |
} else if (path.endsWith("jpeg") || | |
path.endsWith("jpg") || | |
path.endsWith("png")) { | |
remotePath = 'alert/images'; | |
} else { | |
//Audio | |
remotePath = 'alert/audios'; | |
} | |
try { | |
final uploadStream = | |
uploadSingleFile(localPath: path, remotePath: remotePath); | |
streamSubscription = uploadStream.listen( | |
(event) async { | |
if (event.state == TaskState.error) { | |
print( | |
"--------------An error has occurred in TaskState.error -------------"); | |
streamSubscription.cancel(); | |
hasException = true; | |
completer.completeError(Exception()); | |
throw Exception(); | |
} | |
if (event.state == TaskState.canceled) { | |
print( | |
"--------------An error has occurred in TaskState.canceled -------------"); | |
streamSubscription.cancel(); | |
hasException = true; | |
completer.completeError(Exception()); | |
throw Exception(); | |
} | |
if (event.state == TaskState.running) { | |
totalUploaded = totalUploaded + event.bytesTransferred as int; | |
updateUploadProgress = (totalUploaded / totalFilesSize); | |
print( | |
"------------------upload progress is ${event.bytesTransferred}-------------------------"); | |
} | |
if (event.state == TaskState.success) { | |
streamSubscription.cancel(); | |
try { | |
final downloadUrl = await getDownloadUrl( | |
localPath: path, remotePath: remotePath); | |
print( | |
"------------------download url is $downloadUrl---------------------"); | |
if (path.endsWith("mp4") || | |
path.endsWith("3gp") || | |
path.endsWith("avi")) { | |
videoDownloadUrls.add(downloadUrl); | |
} else if (path.endsWith("jpeg") || | |
path.endsWith("jpg") || | |
path.endsWith("png")) { | |
imageDownloadUrls.add(downloadUrl); | |
} else { | |
//Audio | |
audioDownloadUrls.add(downloadUrl); | |
} | |
if (downloadUrl.length > 0) { | |
uploadedCount++; | |
print( | |
"---------------------------total uploaded is ${uploadedCount}--------------------------"); | |
} | |
completer.complete(); | |
} catch (e) { | |
_uploadProgress = 0.0; | |
_error = "Could not upload video, Try again"; | |
updateStatus = RequestStatus.failed; | |
hasException = true; | |
completer.completeError(Exception()); | |
throw Exception(); | |
} | |
} | |
}, | |
onError: (err) { | |
print("---------------Error has occured in stream--------------"); | |
streamSubscription.cancel(); | |
_uploadProgress = 0.0; | |
_error = "Could not upload video, Try again"; | |
updateStatus = RequestStatus.failed; | |
hasException = true; | |
completer.completeError(Exception()); | |
throw Exception(); | |
}, | |
cancelOnError: true, | |
); | |
} catch (e) { | |
print("------error uploaidng to firebase caught in loop"); | |
hasException = true; | |
completer.completeError(Exception()); | |
} | |
/*** Run completer to move on to the next one */ | |
try { | |
await completer.future; | |
} catch (e) { | |
print( | |
"--------------------Error given from completer -----------------"); | |
hasException = true; | |
throw Exception(); | |
} | |
} | |
/**** End loop *****/ | |
if (uploadedCount == bigList.length) { | |
updateUploadProgress = 0.0; | |
/** Upload to alert **/ | |
alertRepository.createInfoAlert( | |
alertCategoryId: alertCategoryId, | |
headline: headline, | |
body: body, | |
location: location, | |
longitude: longitude, | |
latitude: latitude, | |
videos: videoDownloadUrls.join(","), | |
images: imageDownloadUrls.join(","), | |
audio: audioDownloadUrls.join(","), | |
); | |
updateStatus = RequestStatus.success; | |
} | |
// print("download links for video $videoDownloadUrls"); | |
// print("download links for image $imageDownloadUrls"); | |
// print("download links for audio $audioDownloadUrls"); | |
// | |
// ///Upload to alert. | |
// updateUploadProgress = 0.0; | |
// updateStatus = RequestStatus.success; | |
} on ClientException catch (e) { | |
updateUploadProgress = 0; | |
final error = e.message; | |
if (error.contains("Failed host lookup")) { | |
_error = "No Internet Connection"; | |
updateStatus = RequestStatus.noNetwork; | |
} | |
} catch (e) { | |
print("Error while trying to upload media for info alert $e"); | |
_uploadProgress = 0.0; | |
_error = "Could not upload media, Try again"; | |
updateStatus = RequestStatus.failed; | |
} | |
} | |
/** Below is the code that uploads to firebase **/ | |
Stream uploadSingleFile({ | |
required String localPath, | |
required String remotePath, | |
}) { | |
///Bilal | |
///a single file to firebase and get the download links. | |
//Create reference to bucket | |
final ref = FirebaseStorage.instance.ref(remotePath); | |
// String downloadUrl = ""; | |
try { | |
final fileName = path.basename(localPath); | |
final videoRef = ref.child(fileName); | |
final stream = videoRef.putFile(File(localPath)).snapshotEvents; | |
// if (state.state == TaskState.error || | |
// state.state == TaskState.canceled) { | |
// print("No internet connection: could not upload media"); | |
// throw Exception("Could not upload video"); | |
// } | |
// | |
// if (state.state == TaskState.success) { | |
// print("uploaded media"); | |
// String downloadLink = await videoRef.getDownloadURL(); | |
// downloadUrl = downloadLink; | |
// } | |
return stream; | |
} catch (e) { | |
print("unable to upload video other error :: $e"); | |
throw Exception(e); | |
} | |
/** End **/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment