I am a newbie on flutter programming. I am trying to understand async function calls, but got off track.
I want to store data on my device in a secured way. So, when writing the data, the data will be encrypted and when reading the data, the data will be decrypted. The application is not aware that the data is encrypted on the device.
I have made a Storage class which makes use of the SharedPreferences library.
A second functionality of the reading function, is that the application will wait till the data is available.
I wrote something like this
static String getString(String key) {
SharedPreferences prefs = await SharedPreferences.getInstance();
return _decrypt(prefs.getString(key));
}
Now the compiler complains that the await statement is not expected.
When I change the function to an async function returning a Future, it works. However my problem of waiting moves up, to the place where the getString function is called.
Goal: the function shall return the decrypted String and the main thread needs to wait till the data is available.
Added from an answer by the OP that should have been an edit:
Sorry not to be specific enough. My page is loading in it's own thread. Within the thread I want to get the value from the store. The thread cannot continue since the next statement depends on the data from the store.
E.g. I need username and password from the store to be able to login. username and password are stored separately.
Of course I can nest 'then' statements, but I think for readability of the source code it is better to wait till the value is read from the store.