Home > Enterprise >  How to whole json response store in dart model and access that response via dart model instance in f
How to whole json response store in dart model and access that response via dart model instance in f

Time:06-22

I'm trying to store my whole JSON response in the dart model class. but the response is not stored in the model.

I got this error in my debug console

enter image description here

this is my JSON response

{ "status": "success", "message": "retrived successfully", "data": [ { "id": 1, "name": "NEET RAJKOT", "logo": "https://via.placeholder.com/150/92c952" }, { "id": 2, "name": "MEDICAL", "logo": "https://via.placeholder.com/150/771796" }, { "id": 3, "name": "NEET", "logo": "https://via.placeholder.com/150/24f355" } ] }

This code is how to I get response and store in model class

response = await http.get(
          Uri.parse('api_url'),
          headers: {""});

      String data = json.decode(response.body);

      CoursesModel coursesModel = CoursesModel.fromJson(data);

      List<Data> coursesData = coursesModel.data;

This my model class

import 'dart:convert';

import 'package:flutter/foundation.dart';

class CoursesModel {
  String status;
  String message;
  List<Data> data;
  CoursesModel({
    required this.status,
    required this.message,
    required this.data,
  });

 

  

  CoursesModel copyWith({
    String? status,
    String? message,
    List<Data>? data,
  }) {
    return CoursesModel(
      status: status ?? this.status,
      message: message ?? this.message,
      data: data ?? this.data,
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'status': status,
      'message': message,
      'data': data.map((x) => x.toMap()).toList(),
    };
  }

  factory CoursesModel.fromMap(Map<String, dynamic> map) {
    return CoursesModel(
      status: map['status'] as String,
      message: map['message'] as String,
      data: List<Data>.from((map['data'] as List<int>).map<Data>((x) => Data.fromMap(x as Map<String,dynamic>),),),
    );
  }

  String toJson() => json.encode(toMap());

  factory CoursesModel.fromJson(String source) => CoursesModel.fromMap(json.decode(source) as Map<String, dynamic>);

  @override
  String toString() => 'CoursesModel(status: $status, message: $message, data: $data)';

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
  
    return other is CoursesModel &&
      other.status == status &&
      other.message == message &&
      listEquals(other.data, data);
  }

  @override
  int get hashCode => status.hashCode ^ message.hashCode ^ data.hashCode;
}

class Data {
  int id;
  String name;
  String logo;
  Data({
    required this.id,
    required this.name,
    required this.logo,
  });

  

  Data copyWith({
    int? id,
    String? name,
    String? logo,
  }) {
    return Data(
      id: id ?? this.id,
      name: name ?? this.name,
      logo: logo ?? this.logo,
    );
  }

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'id': id,
      'name': name,
      'logo': logo,
    };
  }

  factory Data.fromMap(Map<String, dynamic> map) {
    return Data(
      id: map['id'] as int,
      name: map['name'] as String,
      logo: map['logo'] as String,
    );
  }

  String toJson() => json.encode(toMap());

  factory Data.fromJson(String source) => Data.fromMap(json.decode(source) as Map<String, dynamic>);

  @override
  String toString() => 'Data(id: $id, name: $name, logo: $logo)';

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
  
    return other is Data &&
      other.id == id &&
      other.name == name &&
      other.logo == logo;
  }

  @override
  int get hashCode => id.hashCode ^ name.hashCode ^ logo.hashCode;
}

CodePudding user response:

use json_serializable (https://pub.dev/packages/json_serializable) package.

annotate class with @JsonSerilzable (and field with @JsonKey if needed). see the documentation for details.

CodePudding user response:

I'm giving a lot of effort then I got a solution

1st I change fromJson to fromMap and my response takes as a Map

data = json.decode(coursesResponse.body);

coursesModel = CoursesModel.fromMap(data as Map<String,dynamic>);

coursesData = coursesModel.data!;

2nd Change is in the Model class

old-line code in fromMap Method List has List int

factory CoursesModel.fromMap(Map<String, dynamic> map) {
    return CoursesModel(
      status: map['status'] != null ? map['status'] as String : null,
      message: map['message'] != null ? map['message'] as String : null,
      data: map['data'] != null ? List<Data>.from((map['data'] as List<int>).map<Data?>((x) => Data.fromMap(x as Map<String,dynamic>),),) : null,
   );
}

to change List dynamic

factory CoursesModel.fromMap(Map<String, dynamic> map) {
    return CoursesModel(
      status: map['status'] != null ? map['status'] as String : null,
      message: map['message'] != null ? map['message'] as String : null,
      data: map['data'] != null ? List<Data>.from((map['data'] as List<dynamic>).map<Data?>((x) => Data.fromMap(x as Map<String,dynamic>),),) : null,
   );
}
  • Related