I’m making an attempt to add a picture to Firebase Storage in my Flutter app. Firebase initializes efficiently, and the add begins, but it surely fails halfway with the error:
Error ::
[firebase_storage/unknown] cancelled
flutter: Firebase initialized efficiently!
flutter: Nameless sign-in profitable!
flutter: State: TaskState.working
flutter: Transferred: 0 / 0
flutter: State: TaskState.working
flutter: Transferred: 150 / 2564108
flutter: State: TaskState.working
flutter: Transferred: 2097302 / 2564108
flutter: State: TaskState.working
flutter: Transferred: 2097302 / 2564108
flutter: Add failed: cancelled
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_storage/unknown] cancelled
enter code right here
last ImagePicker _picker = ImagePicker();
XFile? _image;
UploadTask? uploadTask;
bool _isUploading = false;
String imgUrl="";
// Operate to choose a picture from the gallery
Future<void> _pickImage() async {
last pickedFile = await _picker.pickImage(supply: ImageSource.gallery);
if (pickedFile != null) {
setState(() {
_image = pickedFile;
});
}
}
// Operate to add picture to Firebase Storage
Future<void> uploadFile(BuildContext context) async {
if (_image == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content material: Textual content("Please choose a picture first!")),
);
return;
}
last file = File(_image!.path);
if (!file.existsSync()) {
debugPrint("File doesn't exist.");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content material: Textual content("File doesn't exist!")),
);
return;
}
strive {
setState(() {
_isUploading = true;
});
last ref = FirebaseStorage.occasion
.ref()
.baby('recordsdata/${basename(_image!.path)}');
uploadTask = ref.putFile(file);
uploadTask!.snapshotEvents.hear((occasion) {
debugPrint("State: ${occasion.state}");
debugPrint("Transferred: ${occasion.bytesTransferred} / ${occasion.totalBytes}");
if (occasion.state == TaskState.canceled) {
debugPrint("Add was canceled by the person or system.");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content material: Textual content("Add canceled!")),
);
}
});
last snapshot = await uploadTask!.whenComplete(() {});
last downloadUrl = await snapshot.ref.getDownloadURL();
setState(() {
_isUploading = false;
imgUrl = downloadUrl;
});
debugPrint("Add profitable: $downloadUrl");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content material: Textual content("Add profitable!")),
);
} on FirebaseException catch (e) {
setState(() {
_isUploading = false;
});
debugPrint("Add failed: ${e.message}");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content material: Textual content("Add failed: ${e.message}")),
);
} catch (e) {
setState(() {
_isUploading = false;
});
debugPrint("An unknown error occurred: $e");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content material: Textual content("An unknown error occurred: $e")),
);
}
}