如何在 flutter 中使用三元检查 sharedpreferences 中的数据
How to check data in sharedpreferences using ternary in flutter
我想检查 sharedpreferences 中的数据。是否存在或不使用三进制。有没有办法做到这一点?这是代码
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//I want to check something inside sharedpreferences here but I don't have any idea to do that
(something I want to check) ? do something :
isLoading
? Text("Loading...")
: (_image == null)
? Text('In')
: (_data.length == 0)
? Text("Try again")
: fail
? Text("Try again")
: DateFormat("HH:mm:ss")
.format(DateTime.now()) ==
"23:59:59"
? changeTime()
: Text("Success")
],
),
试试这个方法
Take one boolean variable
bool isVisible = true;
now update your isVisible
variable inside getLoginStatus()
method
void getLoginStatus() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String userData = prefs.getString('YourKEY');
if (userData?.isEmpty ?? true) {
setState(() {
isVisible = false;
});
} else {
setState(() {
isVisible = true;
});
}
}
Now you can need to call getLoginStatus()
when you need to check the value in sharedpreferences
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
isVisible ? do something :
isLoading
? Text("Loading...")
: (_image == null)
? Text('In')
: (_data.length == 0)
? Text("Try again")
: fail
? Text("Try again")
: DateFormat("HH:mm:ss")
.format(DateTime.now()) ==
"23:59:59"
? changeTime()
: Text("Success")
],
),
我想检查 sharedpreferences 中的数据。是否存在或不使用三进制。有没有办法做到这一点?这是代码
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//I want to check something inside sharedpreferences here but I don't have any idea to do that
(something I want to check) ? do something :
isLoading
? Text("Loading...")
: (_image == null)
? Text('In')
: (_data.length == 0)
? Text("Try again")
: fail
? Text("Try again")
: DateFormat("HH:mm:ss")
.format(DateTime.now()) ==
"23:59:59"
? changeTime()
: Text("Success")
],
),
试试这个方法
Take one boolean variable
bool isVisible = true;
now update your
isVisible
variable insidegetLoginStatus()
method
void getLoginStatus() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String userData = prefs.getString('YourKEY');
if (userData?.isEmpty ?? true) {
setState(() {
isVisible = false;
});
} else {
setState(() {
isVisible = true;
});
}
}
Now you can need to call
getLoginStatus()
when you need to check the value insharedpreferences
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
isVisible ? do something :
isLoading
? Text("Loading...")
: (_image == null)
? Text('In')
: (_data.length == 0)
? Text("Try again")
: fail
? Text("Try again")
: DateFormat("HH:mm:ss")
.format(DateTime.now()) ==
"23:59:59"
? changeTime()
: Text("Success")
],
),