0

I want to declare a variable that receives the result of asynchronous function.

var eff_histo= await _eff_histo(selectedline);

The await expression can only be used in an asynchronous function

Snowy Coder Girl
  • 5,408
  • 10
  • 41
  • 72
Oussama Ridéne
  • 252
  • 1
  • 4
  • 14

3 Answers3

3

You still are misunderstanding async. The async keyword does not mean "run on another thread".

To push some code onto another thread, you need to do it explicitly, e.g., Task.Run:

await Task.Run(() => Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId));

I have an async/await intro post that you may find helpful.

BIS Tech
  • 17,000
  • 12
  • 99
  • 148
0

I think this is more of a philosophy that a synchronous method should not wait because other important tasks may be affected, time taking async tasks however can take their time thus await makes sense there. Look into this for more information https://github.com/dart-lang/sdk/issues/22269#issuecomment-108458526

humble_wolf
  • 1,497
  • 19
  • 26
-1

you can't used await outside async function ,, try to build your varable localy and declare your varable inside initState

var eff_histo;
@override
void initState() async {
super.initState()
eff_histo= await _eff_histo(selectedline);

}
Kamal Samir
  • 71
  • 1
  • 11
  • 1
    I am pretty sure you cant change the `initState()` method. By changing i refer to adding the `async` tag. A new method should be created and then use the async on that method, then call the new method in the initState() see https://stackoverflow.com/a/51901311/4712391 – Tinus Jackson Jun 13 '19 at 08:55
  • 1
    I can't use async in initState fun ! – Oussama Ridéne Jun 14 '19 at 05:20