Home > Enterprise >  ReadToken() is returning "Instance of 'Future<Object>'"
ReadToken() is returning "Instance of 'Future<Object>'"

Time:06-30

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?

CodePudding user response:

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() which is a syntactic sugar for await().

So, In your case it would be:

readToken().then((value) {
      // Do something with the `value`
    });
  • Related