uploadPhoteChat method
dynamic
uploadPhoteChat(
- XFile selectedXFile
Implementation
uploadPhoteChat(XFile selectedXFile) async {
try {
String fileName = DateTime.now().millisecondsSinceEpoch.toString();
//add random string to file name
fileName = "${fileName}_${Random().nextInt(1000000)}";
//get file extension for XFile path
String fileExtension = selectedXFile.path.split(".").last;
fileName = "$fileName.$fileExtension";
//
File selectFile = File(selectedXFile.path);
final storageRef = FirebaseStorage.instance.ref();
final chatImagesRef = storageRef.child("chat/images/$fileName");
await chatImagesRef.putFile(selectFile);
String imageLink = await chatImagesRef.getDownloadURL();
//send chat
ChatMessage message = ChatMessage(
user: chatEntity.mainUser.toChatUser(),
createdAt: DateTime.now(),
medias: [
ChatMedia(
url: imageLink,
fileName: fileName,
type: MediaType.image,
isUploading: true,
),
],
);
//
sendMessage(message);
} catch (error) {
if (kDebugMode) {
print("Error uploading file ==> $error");
}
}
}