John Jacob
John Jacob

Reputation: 107

How to call this async function in Flutter?

I'm completely new to Flutter and trying to call an asynchronous function. I keep getting

Error: The argument type 'Future<String> Function()'can't be assigned to the parameter type 'String'.

When I try to await the function inside an asynchronous function, it also doesn't work for me. How do I fix this?

 messages.add(ChatMessage(
    messageContent: getCompletionOpenAi(myController.text),
    messageType: "receiver",
));

Upvotes: 0

Views: 184

Answers (1)

Jitesh Mohite
Jitesh Mohite

Reputation: 34210

First get the message using await keyword which wait for async mesaage, also mark function as async.

String msg = await getCompletionOpenAi(myController.text)

 messages.add(ChatMessage(
    messageContent: msg,
    messageType: "receiver",
));

Upvotes: 1

Related Questions