fetchUnacknowledgedAchievements function

Future<UserAchievementListModel> fetchUnacknowledgedAchievements(
  1. String userId,
  2. Config config
)

Implementation

Future<UserAchievementListModel> fetchUnacknowledgedAchievements(
    String userId, Config config) async {
  String path =
      '${constants.API_HOST}/tenant/${config.applicationId}/user-achievements/get-unacknowledged/$userId';

  final response = await http.post(Uri.parse(path),
      body: jsonEncode(<String, String>{
        "application_id": config.applicationId,
        "client_secret": config.clientSecret,
        "client_id": config.clientId,
        "version": config.version,
      }),
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
      });

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    var rb = response.body;

    // store json data into list
    var achievementResponse = jsonDecode(rb) as Map<String, dynamic>;

    // iterate over the list and map each object in list to Img by calling Img.fromJson
    UserAchievementListModel achievementList =
        UserAchievementListModel.fromJson(
      achievementResponse,
    );

    return achievementList;
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to fetch available achievements');
  }
}