Search code examples
flutterdartdart-async

How to convert Future<String> to String


I'm writing an app that needs to load results from a file when it starts, so time consumption is not really an issue here.

Because I don't want the whole code to be asynchronous (because I want everything to be still pretty easy to understand), I'm looking for a way to convert a Future<String > into a normal String.

I would like help at any level: If anyone knows a way to load file contents without asynchronous code, that would also be good.


Solution

  • You can easily do this with await keyword:

    String str = await futureString;
    

    or with then method:

    String str;
    
    futureString.then((result){
      str = result;
    });