callApi method

Future<void> callApi()

Implementation

Future<void> callApi() async {

  var api = ApiLayer(env: ApisModel().dataRequest, bearerToken: widget.model.token);

  List<Map?> messages = [];
  for (var element in _messages) {
    messages.add({
      "name": element.name,
      "message": element.message
    });
  }
  List<Map?> reversedMessages = messages.reversed.toList();
  var data = { "messages": reversedMessages };

  var response = await api.callApi(endpointData: ApisModel().callIA, body: data);
  final result = aiResponseFromMap(response.response);
  setState(() {
    canTyping = true;

    _messages.insert(0,
        ChatMessageWidget(
          bgColor: secondaryColor,
          message: "${result.choices?.first.text?.replaceAll(" \n\n", "")}",
          name: "[${widget.model.aiName}]: ",
          isAI: true,
        )
    );

    updateModel();

  });
}