-1

I've been trying to load a text file into a variable, so that I can check if the user's input is valid. However, I've been having difficulty trying to store the output of the rootBundle.loadString function into a useable String variable, and I'm not sure how to do it.

My code:

Future<String> getFileData(String path) async {
  return rootBundle.loadString(path);
}

String text = getFileData("assets/textfile.txt");

How would I store the contents of a text file in a variable as a string type?

1 Answers1

2

You need to await the loadString method, it's return type is Future<String>.

Future<String> getFileData(String path) async {
  return await rootBundle.loadString(path);
}

String text = await getFileData("assets/textfile.txt");
Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70