0

I am simply saving the string value in FlutterSecureStorage and getting it on other page issue is its printing the value on other page but showing error

First page where I save the string

  if(data['access_token'].length > 2) {
    final storage = new FlutterSecureStorage();
    await storage.write(key: 'token', value: data['access_token']);

    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => MyStatefulWidget()),
    );
  }

Second page where I am getting it

class _EventsState extends State<Events> {

  @override
  initState() async {
    // TODO: implement initState
    final storage = new FlutterSecureStorage();
    String value = await storage.read(key: 'token');
    print(value);

    super.initState();
  }
}

Error is

_EventsState.initState() returned a Future.

Also attaching whole error

enter image description here

nvoigt
  • 75,013
  • 26
  • 93
  • 142
rameez khan
  • 73
  • 1
  • 23
  • 68

1 Answers1

3

state.initState() must be a void method without an async keyword

That is literally what the error message says and it is correct.

You cannot do what you do right now in your initState method. You will need to find a way to save the Future you get from storage.read and wait for it's completion somewhere else.

Most likely using a FutureBuilder is your best option.

See What is a Future and how do I use it?.

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
nvoigt
  • 75,013
  • 26
  • 93
  • 142