1

i am fetching a api response from json how can i join 2 field value to a string list

  {
    "entity_id": "65",
    "user_id": "37",
    "tenancy_random_no_prefix": "C2",
    "tenancy_random_no": "10001",
    "unit_name": "1490",
    "owner_name": "Emaar",
  }
  {
    "entity_id": "66",
    "user_id": "37",
    "tenancy_random_no_prefix": "C2",
    "tenancy_random_no": "10002",
    "unit_name": "1490",
    "owner_name": "arham",
  }

how to get a List = [owner_name+tenancy_random_no]

which will come like this

['Emaar10001','arham10002']
1
  • I suggest that you are able to parse data into entity, then you can try this: data.map((e) => '${e. owner_name}${e.tenancy_random_no}').toList(). Commented Sep 24, 2020 at 7:47

2 Answers 2

3

Your API response contains a list of maps from string to dynamic, so List<Map<String, dynamic>>. You can use functional programming technique very easily here using map function like in the first solution. Alternatively, you can also use forEach or classical for-in loop, depending on your preference. All solutions are completely explanatory by themselves and by checking the used functions' docs if necessary.

final apiResponse = [
  {
    "entity_id": "65",
    "user_id": "37",
    "tenancy_random_no_prefix": "C2",
    "tenancy_random_no": "10001",
    "unit_name": "1490",
    "owner_name": "Emaar",
  },
  {
    "entity_id": "66",
    "user_id": "37",
    "tenancy_random_no_prefix": "C2",
    "tenancy_random_no": "10002",
    "unit_name": "1490",
    "owner_name": "arham",
  }
];

// Functional with map
final fsolution = apiResponse.map((Map<String, dynamic> m) => "${m['owner_name']}${m['tenancy_random_no']}").toList();

// Functional with forEach
final fsolution2 = <String>[];
apiResponse.forEach((Map<String, dynamic> m) => fsolution2.add("${m['owner_name']}${m['tenancy_random_no']}"));

// Iterative
final isolution = <String>[];
for (final m in apiResponse) {
  isolution.add("${m['owner_name']}${m['tenancy_random_no']}");
}
Sign up to request clarification or add additional context in comments.

Comments

2
  final source = [
    {
      "entity_id": "65",
      "user_id": "37",
      "tenancy_random_no_prefix": "C2",
      "tenancy_random_no": "10001",
      "unit_name": "1490",
      "owner_name": "Emaar",
    },
    {
      "entity_id": "66",
      "user_id": "37",
      "tenancy_random_no_prefix": "C2",
      "tenancy_random_no": "10002",
      "unit_name": "1490",
      "owner_name": "arham",
    }
  ];

  var result = [];
  for (var entity in source) {
    result.add(['${entity['owner_name']} ${entity['tenancy_random_no']}']);
  }
  print(result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.