uploadPhoteChat method

Future<void> uploadPhoteChat(
  1. XFile selectedXFile
)

Uploads selectedXFile via the backend and sends it as an image message.

Implementation

Future<void> 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";
    //
    String imageLink = await chatEntity.backend.uploadImage(
      selectedXFile.path,
      fileName,
    );
    //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");
    }
  }
}