Reputation: 87
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.
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.
Upvotes: 3
Views: 17530
Reputation: 524
Just call in this way here is My function & Set state is Imp !
checkInCart()async{
bool pre=await storage.containsKey(key: '${widget.eventIndex}');
if(pre) {
setState(() {
isavail=true;
favorite=true;
});
}
}
and call it in
@override
void initState() {
// TODO: implement initState
super.initState();
checkInCart();
}
This must work Welcome in advance !
Upvotes: 0
Reputation: 408
Future strUser() async {
SharedPreferences sharedPreferences = await
SharedPreferences.getInstance();
return sharedPreferences.getInt('strUser');
}
init(){
await SharedPreferenceFunction().strUser().then((value) {
setState(() {
this.strUser = value;
});
});
}
Upvotes: 0
Reputation: 15079
You should never block the main thread, because it blocks app operations. Your intialization might take long time and system can kill your apps if necessary.
To solve your problem, it is better not to block main thread, and it can be done by creating a loading screen, wait for data loading to complete, and show the final app screen.
The code prototype could be like following:
class HomePage extends StatefulWidget {
@override
HomePageState createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
String data;
@override
void initState() {
super.initState();
loadData().then((data) {
setState(() {
this.data = data;
});
});
}
Future loadData() async {
data = 'xxx'; // load your data from SharedPreferences
return data;
}
@override
Widget build(BuildContext context) {
if (data == null) {
loadData();
return LoadingScreen();
} else {
return MainScreen();
}
}
}
Upvotes: 9
Reputation: 2556
You have to add the keywords async and Future as follows:
static Future<String> getString(String key) async { // ------> here
SharedPreferences prefs = await SharedPreferences.getInstance();
return _decrypt(prefs.getString(key));
}
and to consume this asynchronous method you must do it in the following way:
getString("someKey").then((valueString) => print(valueString));
Upvotes: 2