Saya memiliki daftar objek dalam bentuk json yang berasal dari API. Objek-objek ini memiliki beberapa atribut, masalahnya adalah beberapa objek memiliki atribut yang hilang. Pertanyaan saya adalah: Apakah mungkin untuk mengurai data ini di Flutter/Dart atau tidak? ATAU Saya harus mengubah ke respons API sehingga tidak ada atribut yang hilang di dalamnya.

DATA JSON:

[
    {
        "names": "Jean lewis  Mbayabu ",
        "users_name": "jeanlewis",
        "avt": "Image URL",
        "id": "32",
        "title": "Canada",
        "category": "student"
    },
    {
        "names": "Abhi Vish",
        "users_name": "abhi",
        "avt": "Image URL",
        "id": "59",
        "category": "student"
    },
    {
        "names": "Julie Mabengi",
        "users_name": "julie",
        "avt": "Image URL",
        "id": "116"
    }
]
-1
Ahmad Rehan 25 Mei 2021, 17:22

1 menjawab

Jawaban Terbaik

Berikut adalah contoh tentang cara melakukannya. Saya telah membuat title atau category nullable karena kami tidak selalu memiliki dua bidang ini:

import 'dart:convert';

const jsonString = '''
[
    {
        "names": "Jean lewis  Mbayabu ",
        "users_name": "jeanlewis",
        "avt": "Image URL",
        "id": "32",
        "title": "Canada",
        "category": "student"
    },
    {
        "names": "Abhi Vish",
        "users_name": "abhi",
        "avt": "Image URL",
        "id": "59",
        "category": "student"
    },
    {
        "names": "Julie Mabengi",
        "users_name": "julie",
        "avt": "Image URL",
        "id": "116"
    }
]
''';

class SearchedUser {
  final String names;
  final String users_name;
  final String avt;
  final int id;
  final String? title;
  final String? category;

  const SearchedUser(
      {required this.names,
      required this.users_name,
      required this.avt,
      required this.id,
      required this.title,
      required this.category});

  factory SearchedUser.fromJson(Map<String, dynamic> json) => SearchedUser(
      names: json['names'] as String,
      users_name: json['users_name'] as String,
      avt: json['avt'] as String,
      id: int.parse(json['id'] as String),
      title: json['title'] as String?,
      category: json['category'] as String?);

  @override
  String toString() => 'names: $names, '
      'users_name: $users_name, '
      'avt: $avt, '
      'id: $id, '
      'title: $title, '
      'category: $category';
}

void main() {
  final jsonList = jsonDecode(jsonString) as List<dynamic>;
  final searchedUsers = [
    for (final map in jsonList.cast<Map<String, dynamic>>())
      SearchedUser.fromJson(map)
  ];

  searchedUsers.forEach(print);
  // names: Jean lewis  Mbayabu , users_name: jeanlewis, avt: Image URL, id: 32, title: Canada, category: student
  // names: Abhi Vish, users_name: abhi, avt: Image URL, id: 59, title: null, category: student
  // names: Julie Mabengi, users_name: julie, avt: Image URL, id: 116, title: null, category: null
}
0
julemand101 25 Mei 2021, 14:56