Search
 
SCRIPT & CODE EXAMPLE
 

DART

how to parse json with missing key in lfutter

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?,
      // title: json['title'] ?? "defaultTitle", // default title if field is absent or null
      category: json['category'] as String?);
      // category: json['category'] ?? "defaultCategory"); // default category if field is absent or null

  @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
}
Comment

PREVIOUS NEXT
Code Example
Dart :: inkwell not splashing in stack 
Dart :: dart rob cross axis align not work 
Dart :: how to add image to flutter 
Dart :: functions in dart 
Dart :: dart uzunlik 
Swift :: swift uiview add tap gesture 
Swift :: tellraw minecraft 
Swift :: swift 5 get current year 
Swift :: swift share with 
Swift :: swift has Top Notch 
Swift :: get device name swift 
Swift :: custom screen presentation controller coner radius swift 
Swift :: remove divider list swiftui 
Swift :: textfield style swiftui own 
Swift :: swift struct field default value 
Swift :: swift access appdelegate from viewcontroller 
Swift :: remove back button text nav bar swift 
Swift :: swift clear badge number 
Swift :: remove padding hstack swiftui 
Swift :: and in swif 
Swift :: get address from latitude and longitude in swift 
Swift :: swift alert toast 
Swift :: activity indicator swiftui 
Swift :: swft ui image 
Swift :: swift iterate through string 
Swift :: how to Not bool bindng swiftui 
Swift :: and or in swift 
Swift :: swift make enum inspectable 
Swift :: Optional & Default Parameter Swift 
Swift :: move view controller to make space for keyboard swift 5 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =