0

I wish to get a string so as to pass to the second activity in flutter from the async function below:

Future<String> getString() async {
    return await readFromFile("./abc.txt").toString();

}

My problem is getting hold of a string from the function above and use it inside onPressed() funtion, I want to navigate to the next page and wish to pass the string along to the next page. Whenever I try doing this, it gives me something like 'instance of Future...' instead of the actual string.

4
  • Yout function return a future, you should await for it String response = await getString(); So then pass the response string to the navigator Commented Dec 2, 2019 at 15:35
  • Thanks, I'd attempted this, but it's kind of ways too slow. Commented Dec 2, 2019 at 16:10
  • The slowness its caused by the readFile. Are you testing this over the emulator? Commented Dec 2, 2019 at 16:17
  • Yes, I am testing it with an emulator Commented Dec 3, 2019 at 7:14

1 Answer 1

1

You can either split the assignment or add parenthesis, because right now you actually try to await on toString.

Future<String> getString() async {
  Something content = await readFromFile("./abc.txt");
  return content.toString();
}
Future<String> getString() async {
  return (await readFromFile("./abc.txt")).toString();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.