'flutter: A value of type 'Future<String>' can't be assigned to a variable of type 'String' [duplicate]

I don't understand how to lead to one type my function:

Future<String> getToken(String login) async {
    final data = await (select(authInfos)
          ..where((tbl) => tbl.login.equals(login)))
        .getSingle();
    return data.token;
  }

row with error:

String token = myDatabase.authInfosDao.getToken(login);


Solution 1:[1]

For functions that returns a future result you will need to put await on calling it to ensure that the variable will get the return of the said function.

Here's the Syntax:

String token = await myDatabase.authInfosDao.getToken(login);

Update:

Future<void> initState() async {
    // TODO: implement initState
    String token = await myDatabase.authInfosDao.getToken(login);
    super.initState();
}

Note: When using await expression it should be used on an async funtion.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1