sendMessage method

  1. @override
Future<void> sendMessage([
  1. String? messageDefault
])
override

Implementation

@override
Future<void> sendMessage([String? messageDefault]) async {
  try {
    String message = messageDefault ?? textEditingController.text;
    assert(message.isNotEmpty, "Message cannot be empty");
    if (message.isEmpty) return;
    messages.add(MessageChat(
        content: message,
        isSentByMe: true,
        created: _params.formatDataUtil.getCurrentHour(),
        role: "user"));
    textEditingController.clear();
    isLoading = true;
    notifyListeners();
    scrollToEnd();
    final responseMessage =
        await _apiDatasource.sendMessage(message: message);
    messages.add(responseMessage);
  } catch (e) {
    _handleUnexpectedError('Unexpected error occurred: $e');
  } finally {
    isLoading = false;
    notifyListeners();
    Future.delayed(Durations.medium4, scrollToEnd);
  }
}