0

I am having this type of response from API

{
    "success": 1,
    "data": [
        {
            "id": 539,
            "user_id": 3115,
            "amount": 5788,
            "payment_gateway": "bank",
            "message": "chchhb",
            "status": "waiting",
            "source": "everyone",
            "created_ts": "2019-12-19 13:41:17",
            "processed_ts": null
        },
      ]
}

this is my model.dart

class Model {
  final String status;
  final List<String> data;

  Model({
    this.status,
    this.data
  });

  factory Model.fromJson(Map<String, dynamic> parsedJson) {
    var data  = parsedJson['data'];
     List<String> dataList = data.cast<String>();
    return Model(
      status: parsedJson['status'],
      data: dataList
    );
  }

}

This is how I am fetching data from the model

 Future<List<String>> getWithdrawals() async {
      final userModel = Provider.of<UserModel>(context);
      var userId = userModel.user.id;
      Map<String, String> requestHeaders = {'Content-type': 'application/json'};
      var body = {
        'id': userId,
      };
      final response = await http.post(url);
      if (response.statusCode == 200){
        var jsonresponse = json.decode(response.body);
        var withdrawals = WithdrawalModel.fromJson(jsonresponse);
        return withdrawals.data;
      } else {
        print("Error" + response.body);
        return null;
      }
    }

I am not able to display the data on the screen It is giving me an error like

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in typecast

In FutureBuilder<List> I am not able to get data I don't know where I am doing a mistake Please help...

2
  • Show your FutureBuilder widget Commented Dec 20, 2019 at 8:42
  • Tools like This or This, would be helpful. Commented Dec 20, 2019 at 9:54

3 Answers 3

2

I think your response type is not correct currently it's List<String> but it should be like List<Details> and you need to create another model class named Details (Or you can change the name). You just put your response here. It will generate dart class models for you.

NOTE: you have to remove extra comma from your response to use model generator then your response will be like.

{
    "success": 1,
    "data": [
        {
            "id": 539,
            "user_id": 3115,
            "amount": 5788,
            "payment_gateway": "bank",
            "message": "chchhb",
            "status": "waiting",
            "source": "everyone",
            "created_ts": "2019-12-19 13:41:17",
            "processed_ts": null
        }
    ]
}

Edit:

For example in your above model just change List type string to Details(Model) and vise-versa :

class Model {
  final String status;
  final List<Details> data; // Update string with model

}
Sign up to request clarification or add additional context in comments.

2 Comments

Let me know if you have any query.
I am not getting how to figure it out can you help me ?
0

Try this method

Future <PaymentReply> getPayment() async {
  final userModel = Provider.of<UserModel>(context);
  var userId = userModel.user.id;
  try {
    final response = await http.post(url,
      headers: {
        HttpHeaders.contentTypeHeader: "application/json"
      },
      body: '{\'id\':\''+ userId+'\'}', // You are not sending body in your code so check if it needs to be sent.
    );
    print(response.body); //CHECK IF THIS IS CORRECT RESPONSE AS EXPECTED
    PaymentReply reply =  paymentReplyFromJson(response.body);
    return reply;
  }on Exception catch (e){
    print(e);
    return PaymentReply();
  }
}

with my modal

// To parse this JSON data, do
//
//     final paymentReply = paymentReplyFromJson(jsonString);

import 'dart:convert';

PaymentReply paymentReplyFromJson(String str) => PaymentReply.fromJson(json.decode(str));

String paymentReplyToJson(PaymentReply data) => json.encode(data.toJson());

class PaymentReply {
    int success;
    List<Datum> data;

    PaymentReply({
        this.success,
        this.data,
    });

    factory PaymentReply.fromJson(Map<String, dynamic> json) => PaymentReply(
        success: json["success"],
        data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "success": success,
        "data": List<dynamic>.from(data.map((x) => x.toJson())),
    };
}

class Datum {
    int id;
    int userId;
    int amount;
    String paymentGateway;
    String message;
    String status;
    String source;
    String createdTs;
    String processedTs;

    Datum({
        this.id,
        this.userId,
        this.amount,
        this.paymentGateway,
        this.message,
        this.status,
        this.source,
        this.createdTs,
        this.processedTs,
    });

    factory Datum.fromJson(Map<String, dynamic> json) => Datum(
        id: json["id"],
        userId: json["user_id"],
        amount: json["amount"],
        paymentGateway: json["payment_gateway"],
        message: json["message"],
        status: json["status"],
        source: json["source"],
        createdTs: json["created_ts"],
        processedTs: json["processed_ts"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "user_id": userId,
        "amount": amount,
        "payment_gateway": paymentGateway,
        "message": message,
        "status": status,
        "source": source,
        "created_ts": createdTs,
        "processed_ts": processedTs,
    };
}

1 Comment

It was the typing mistake @Dev
0

My model was incorrect. Solved by this model...

class WithdrawalModel {
  final String status;
  final String amount;
  final List<Data> data;

  WithdrawalModel({
    this.status,
    this.amount,
    this.data
  });

  factory WithdrawalModel.fromJson(Map<String, dynamic> parsedJson) {
    var list  = parsedJson['data'] as List;
    List<Data> dataList = list.map((i) => Data.fromJson(i)).toList();
    return WithdrawalModel(
      status: parsedJson['status'],
      amount: parsedJson['amount'],
      data: dataList
    );
  }
}

class Data {
  int id;
  int amount;
  String message;
  String status;
  String source;
  String createdTs;

  Data({
    this.id,
    this.amount,
    this.message,
    this.status,
    this.source,
    this.createdTs
  });

    Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    amount = json['amount'];
    message = json['message'];
    status = json['status'];
    source = json['source'];
    createdTs = json['created_ts'];
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.