loadMessages method

Future<void> loadMessages(
  1. int chatBotId
)

Implementation

Future<void> loadMessages(int chatBotId) async {
  _updateState(_state.copyWith(loading: true));
  try {
    final response = await Future.wait(
      [
        chatService.loadMessages(chatBotId),
        chatService.loadQuestions(chatBotId),
      ],
    );
    final chatBotMessages = response
        .map((e) => e.data)
        .expand((element) => element)
        .map((e) => e.toChatBotMessage())
        .groupListsBy((element) => element.key)
        .values
        .toList();
    _updateState(_state.copyWith(loading: false));
    _state = _state.copyWith(messages: chatBotMessages);
    listKey.currentState?.insertAllItems(0, _state.visibleMessages.length - 1);
  } catch (e) {
    _updateState(_state.copyWith(error: e.toString(), loading: false));
  }
}