0

ReadToken() is returning "Instance of 'Future'"

I was following this tutorial on the Flutter Docs: https://docs.flutter.dev/cookbook/persistence/reading-writing-files.

So, my problem is that, if I just run ReadToken() without running the create function then the ReadToken() function always returns "Instance of 'Future'". Note: I made some changes to the ReadToken() function, like the name. The function is below.

Future<Object> readToken() async {
try {
  final file = await _localFile;

  // Read the file
  final contents = await file.readAsString();

  return contents;
} catch (e) {
  // If encountering an error, return 0
  return 0;
}
  }
}

Is there anything that I'm doing wrong or anything that I should change?

MendelG
  • 14,885
  • 4
  • 25
  • 52

1 Answers1

1

You have to await readToken(). If you continue reading the documentation by the complete example section, it shows this example:

@override
  void initState() {
    super.initState();
    widget.storage.readCounter().then((value) {
      setState(() {
        _counter = value;
      });
    });
  }

It's using .then() instead of await, which await is a syntactic sugar for .then()

So, In your case it would be:

readToken().then((value) {
      // Do something with the `value`
    });
MendelG
  • 14,885
  • 4
  • 25
  • 52