Home > Net >  How to call this async function in Flutter?
How to call this async function in Flutter?

Time:06-19

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",
));

CodePudding user response:

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",
));
  • Related