1

I have a data that I pulled from json. I want to receive and process this data in the following format. When I try manually, I get the error with the actual service data while getting the output I want. My manual attempt:

void main(List<String> args) {
  final map = <Map<String, dynamic>>[
    {'MAH_ID': 10, 'MAH_AD': 'AŞAĞI', 'ILCE_ID': 1, 'ILCE_AD': 'SARI'},
    {'MAH_ID': 11, 'MAH_AD': 'YUKARI', 'ILCE_ID': 1, 'ILCE_AD': 'SARI'},
    {'MAH_ID': 12, 'MAH_AD': 'SAĞ', 'ILCE_ID': 1, 'ILCE_AD': 'SARI'},
    {'MAH_ID': 13, 'MAH_AD': 'SOL', 'ILCE_ID': 1, 'ILCE_AD': 'SARI'},
    {'MAH_ID': 14, 'MAH_AD': 'LOW', 'ILCE_ID': 2, 'ILCE_AD': 'KIRMIZI'},
    {'MAH_ID': 15, 'MAH_AD': 'MİD', 'ILCE_ID': 2, 'ILCE_AD': 'KIRMIZI'},
    {'MAH_ID': 16, 'MAH_AD': 'HİGH', 'ILCE_ID': 2, 'ILCE_AD': 'KIRMIZI'},
    {'MAH_ID': 17, 'MAH_AD': 'ULTRAHİGH', 'ILCE_ID': 2, 'ILCE_AD': 'KIRMIZI'}
  ];

  final mappedValues = <Map<String, List<dynamic>>>[];

  for (final element in map) {
    final ilceAd = element['ILCE_AD'];
    final mahAd = element['MAH_AD'];

    final contains =
        mappedValues.where((e) => e.keys.contains(ilceAd)).toList();

    if (contains.isNotEmpty) {
      if (contains.first.isNotEmpty) {
        if (contains.first.containsKey(ilceAd)) {
          contains.first[ilceAd]!.add(mahAd);
        }
      }
    } else {
      mappedValues.add({
        ilceAd as String: [mahAd]
      });
    }
  }

  print(mappedValues);
}

success output {SARI: [AŞAĞI, YUKARI, SAĞ, SOL]}, {KIRMIZI: [LOW, MİD, HİGH, ULTRAHİGH]}]

my model class

import 'dart:convert';

List<Mahalle> mahalleFromJson(String str) =>
    List<Mahalle>.from(json.decode(str).map((x) => Mahalle.fromJson(x)));

String mahalleToJson(List<Mahalle> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Mahalle {
  Mahalle({
    this.mahId,
    this.mahAdi,
    this.mahIdDbb,
    this.ilceId,
    this.ilceAdi,
    this.ilceIdDbb,
  });

  int? mahId;
  String? mahAdi;
  int? mahIdDbb;
  int? ilceId;
  String? ilceAdi;
  int? ilceIdDbb;

  factory Mahalle.fromJson(Map<String, dynamic> json) => Mahalle(
        mahId: json["MAH_ID"],
        mahAdi: json["MAH_ADI"],
        mahIdDbb: json["MAH_ID_DBB"],
        ilceId: json["ILCE_ID"],
        ilceAdi: json["ILCE_ADI"],
        ilceIdDbb: json["ILCE_ID_DBB"],
      );

  Map<String, dynamic> toJson() => {
        "MAH_ID": mahId,
        "MAH_ADI": mahAdi,
        "MAH_ID_DBB": mahIdDbb,
        "ILCE_ID": ilceId,
        "ILCE_ADI": ilceAdi,
        "ILCE_ID_DBB": ilceIdDbb,
      };
}

method where i pull json data

Future getApiData() async {
    try {
      final response = await http.get(url);
      if (response.statusCode == 200) {
        var result =
            mahalleFromJson(response.body).cast<Map<String, dynamic>>();

        final mappedValues = <Map<String, List<dynamic>>>[];

        for (final element in result) {
          final ilceAd = element['ilceAdi'];
          final mahAd = element["mahAdi"];

          final contains = mappedValues
              .where((element) => element.keys.contains(ilceAd))
              .toList();

          if (contains.isNotEmpty) {
            if (contains.first.isNotEmpty) {
              if (contains.first.containsKey(ilceAd)) {
                contains.first[ilceAd]!.add(mahAd);
              }
            }
          } else {
            mappedValues.add({
              ilceAd as String: [mahAd]
            });
          }
        }
        print(mappedValues);
}

the error i got: type 'Mahalle' is not a subtype of type 'Map<String, dynamic>' in type cast

Thanks in advance for your help

1 Answer 1

1

element in your for loop is an Mahalle Item, so if you want to access ilceAd or mahAd you should do this:

var result = mahalleFromJson(response.body); //<--- change this

final mappedValues = <Map<String, List<dynamic>>>[];

for (final element in result) {
          final ilceAd = element.ilceAdi; //<--- change this
          final mahAd = element.mahAdi; //<--- change this

          final contains = mappedValues
              .where((element) => element.keys.contains(ilceAd))
              .toList();

          if (contains.isNotEmpty) {
            if (contains.first.isNotEmpty) {
              if (contains.first.containsKey(ilceAd)) {
                contains.first[ilceAd]!.add(mahAd);
              }
            }
          } else {
            mappedValues.add({
              ilceAd as String: [mahAd]
            });
          }
        }
Sign up to request clarification or add additional context in comments.

3 Comments

I can't get such access. Because the result is List<Map> while the element is just Map
I finally got it done mate. There was no need for the cast process above, it was solved by the method you said when you deleted it. Thank you so much.
glad to hear that. appreciate it if you up vote me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.