Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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`
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading