Home > Software design >  How to comparison between data from MySQL with Shared Preferences in flutter
How to comparison between data from MySQL with Shared Preferences in flutter

Time:08-04

I was save data in MySQL database and show it in my app flutter. Also I was save same id into Shared Preferences file. Now I'm trying to check if the data (ID) I get from the database is similar to (ID) in Shared Preferences or not.

Code:

   Future ApiNotficationApp() async {
    final String url = '****************';
    var response = await http.post(Uri.parse(url));
    var responsebody=jsonDecode(response.body);
    if (responsebody.length >0){
      for (int i = 0; i < responsebody.length; i  ) {
          if (responsebody[i]['id'] == UserSimplePreferences.getNotficationAppID() ){

            print('\x1B[31m${'Equal'}\x1B[0m');
            break;
          }else{
            UserSimplePreferences.setNotficationApp('');
            UserSimplePreferences.setNotficationAppID(responsebody[i]['id'].toString());
            print('\x1B[33m${'Not Equal'}\x1B[0m');
            print('\x1B[33m${UserSimplePreferences.getNotficationAppID()}\x1B[0m');
            break;
          }
      }
      return responsebody;
    }else{

    }
  }


responsebody

[{id: 86, title: title, body: Body, DateSend: 1-5-2022, IsWatched: false}, {id: 85, title: title, body: Body, DateSend: 1-5-2022, IsWatched: false}, {id: 84, title: title, body: Body, DateSend: 1-5-2022, IsWatched: false}, {id: 64, title: title, body: Body, DateSend: 1-5-2022, IsWatched: false}]

print Shared Preferences

I/flutter ( 9457): Not Equal I/flutter ( 9457): 86

Now I get a result that's not equal, but the data is there, and it's equal.

How can I solve this problem?

CodePudding user response:

Not sure how you have implemented usersimplePreferences. But since you have added that its shared preferences. You may have to await it.

String? notificationAppId = await UserSimplePreferences.getNotficationAppID() ?? "";

Then usethis in the condition like

if ((responsebody[i]['id']).toString() == notificationAppId.toString()){
 ...
}
  • Related