I have a scenario, I have a login page on my app when a user logins in, the app waits for a response from the api, the api response can come in 2 ways. On way with the data as empty or with a growable list. Eg
{
"id": "ea6d156a-ac2e-49b1-b006-1cf2c9092224",
"auth_token": "token",
"email": "[email protected]",
"gender": male,
"fullname": "tester",
"contactNo": "08056582232",
"participants": [
{
"participantid": "055556666",
"dateobirth": "1983-10-12T00:00:00",
"nextofkinname": "Tester",
"nextofkinemail": "[email protected]",
"nextofkinphonenumber": "1234567",
"gender": "female",
"contactnumber": "8900000",
"fullname": "Participant Two",
"userid": "ea6d156a-ac2e-49b1-b006-1cf2c9092224",
"schedules": [
{
"id": 74436,
"followUpStageId": 1,
"windowStart": "2022-01-28T00:00:00",
"windowEnd": "2022-01-30T00:00:00",
"showUp": 0,
"showUpDate": null,
"vaxDose": 0,
}
]
}
or it can come as
{
"id": "afc7e6ff-878a-418b-9a2f-6f7fe2240085",
"auth_token": "",
"email": "[email protected]",
"gender": "male",
"fullname": "tester",
"contactNo": "1234567",
"participants": []
}
Am storing the values in a shared preference, and using them in different places in the app.
I want to check if participants: [] had data if it has data take the user directly to the home screen if is empty the go through the process of adding a participant. How can I check if the size of the participant is zero or not?
this is what I have so far
void getLoggedInStatus() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
isLoggedIn = prefs.getBool('isLoggedIn') ?? false;
gotData = prefs.getString("participants") ??[]; // get the data from prefs
print(gotData);
double data = jsonDecode(gotData);// decode and get size
print(data);
var token = prefs.get("auth_token") ?? "";
var url = Uri.parse(Api.PARTICIPANT_SCHEDULER);
debugPrint("user is logged in: $isLoggedIn");
if (isLoggedIn) {
try {
// try accessing anything from the server
var response = await http.get(url,
headers: {"authorization": "Bearer $token"});
// token expiration check
// if 401 is not returned, the token is still valid. skip authentication
if (response.statusCode != 401) {
if (data.size !=0) { //this doesnt work, it skips the if statements and does nothing
WADashboardScreen().launch(context); //go to home screen if not empty
} else {
WAAddCredentialScreen().launch(context); //add user details if it is empty
}
}
} on SocketException catch (_) {}
}
}
How am saving to the shared prefs
SharedPreferences prefs = await SharedPreferences.getInstance();
var parse = jsonDecode(response.body);
prefs.setBool("isLoggedIn", true);
await prefs.setString('email', parse["email"]);
await prefs.setString('auth_token', parse["auth_token"]);
await prefs.setString('gender', parse["gender"]);
await prefs.setString('participants', (jsonEncode(parse["participants"]))) ;
await prefs.setString('fullname', parse["fullname"]);
await prefs.setString('id', parse["id"]);
Have been scratching my head for a while now, how can I check the size or check if participants has data or not and then proceed to the different screens? Any help is appreciated
CodePudding user response:
var body=json.decode(response.body);
if(body["participants"].isEmpty){
...
}
should help u
CodePudding user response:
In
await prefs.setString('participants', (jsonEncode(parse["participants"])));
You are doing two wrong things here.
You are encoding & parsing json which you are supposed itself a
String?You are storing
List<dynamic>as aStringwhich is not the optimal way.
You might want to use other packages to store participants. SharedPreference is not optimal for every use case.
