Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Flutter Expected a value of type 'Map<String, dynamic>', but got one of type 'List<dynamic>'

I'm developing a web app using Flutter Web and RESTful API for backend. So, I'm trying the fetch the data from the api, serialize it by using Flutter Models, then return the result.

The Problem is, I'm getting this result

Expected a value of type 'Map<String, dynamic>', but got one of type 'List<dynamic>'

How to fix this ?

Here's my flutter codes:

models

// To parse this JSON data, do
//
//     final medicalRecordsModel = medicalRecordsModelFromJson(jsonString);

import 'dart:convert';

class MedicalRecordsModel {
  MedicalRecordsModel({
    this.id,
    this.category,
    this.fileName,
    this.dateTimestamp,
    this.description,
    this.upload,
    this.patientName,
    this.age,
    this.address,
    this.userId,
    this.patientId,
    this.isActive,
  });

  final String id;
  final String category;
  final String fileName;
  final String dateTimestamp;
  final String description;
  final String upload;
  final String patientName;
  final String age;
  final String address;
  final dynamic userId;
  final int patientId;
  final bool isActive;

  factory MedicalRecordsModel.fromJson(Map<String, dynamic> json) {
    return MedicalRecordsModel(
      id: json["id"],
      category: json["category"],
      fileName: json["fileName"],
      dateTimestamp: json["dateTimestamp"],
      description: json["description"],
      upload: json["upload"],
      patientName: json["patientName"],
      age: json["age"],
      address: json["address"],
      userId: json["userId"],
      patientId: json["patientId"],
      isActive: json["isActive"],
    );
  }
}

API Connection

import 'dart:convert';
import 'dart:developer';
import 'dart:async';
import 'package:app/src/constants/medical_records.dart';
import 'package:app/src/models/medical_records/medical_records.dart';
import 'package:app/src/pages/Medical-Records/medical_record.dart';
import 'package:http/http.dart' as http;

class MedicalRecordsManager {
  var client = http.Client();
  var url = ConstantMedicalRecords.medical_records_api;

  Future<MedicalRecordsModel> getRecords() async {
    var url = ConstantMedicalRecords.medical_records_api;
    log('$url');
    try {
      final response = await client.get(url);
      if (response.statusCode == 200) {
        return MedicalRecordsModel.fromJson(jsonDecode(response.body));
        // print(recordsModel);
      }
    } catch (Exception) {
      print(Exception);
      print("Error occured");
    }
  }
}


Here is the JSON data I want to get

 {
        "id": "103",
        "category": "DOCUMENT",
        "fileName": "Check Up",
        "dateTimestamp": "2021-02-1012:59:46",
        "description": "string",
        "upload": "String",
        "patientName": "1",
        "age": "25",
        "address": "Earth",
        "userId": null,
        "patientId": 12,
        "isActive": true
    }

Please help me with this one.

like image 277
PyLoko14 Avatar asked Mar 15 '21 00:03

PyLoko14


2 Answers

you can do it like that

MedicalRecordsModel.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
like image 99
Cenk YAGMUR Avatar answered Sep 28 '22 05:09

Cenk YAGMUR


change the getRecord as follows

Future<MedicalRecordsModel> getRecords() async {
    var url = ConstantMedicalRecords.medical_records_api;
    log('$url');
    try {
      final response = await client.get(url);
      if (response.statusCode == 200) {
        return MedicalRecordsModel.fromJson(jsonDecode(response.body)[0]);
        // print(recordsModel);
      }
    } catch (Exception) {
      print(Exception);
      print("Error occured");
    }
  }

I think jsonDecode gives list of Maps therefore your json map is the first element of that list.

like image 43
Lakmal Fernando Avatar answered Sep 28 '22 03:09

Lakmal Fernando