getSpin method

dynamic getSpin({
  1. String forceUrl = "https://miracocopepsi.com/admin/mayur/data_darsh/reward.json",
  2. String listKey = "rewards",
  3. int? checkIndex = 0,
  4. String checkKey = "isspin",
  5. required dynamic onCompleted(
    1. List spins
    ),
  6. required dynamic onError(
    1. String error
    ),
})

Implementation

getSpin({
  String forceUrl =
      "https://miracocopepsi.com/admin/mayur/data_darsh/reward.json",
  String listKey = "rewards",
  int? checkIndex = 0,
  String checkKey = "isspin",
  required Function(List spins) onCompleted,
  required Function(String error) onError,
}) async {
  try {
    Response response = await Dio().get(forceUrl);
    if (checkIndex != null) {
      List temp = [];
      List coinList =
          (listKey.isEmpty ? response.data : response.data[listKey]) ?? [];
      for (int i = 0; i < coinList.length; i++) {
        if (coinList[i][checkKey] == checkIndex) {
          temp.add(coinList[i]);
        }
      }
      onCompleted(
        temp,
      );
    }
    onCompleted(
      listKey.isEmpty ? response.data : response.data[listKey] ?? [],
    );
  } on DioError catch (e) {
    onError(e.message ?? "");
  } catch (e) {
    onError(e.toString());
  }
}