提问者:小点点

如何修复类型“列表<动态>”不是“字符串”类型的子类型


目前我从我的代码中得到了这种问题。我试图不断解决这个问题。但是无法资助这个问题的解决。

Json代码

{
    "id": 22,
    "client_need_id": 46,
    "clientNeed": {
      "id": 46,
      "client_id": 29,
      "perpose": "Pooja",
      "details": "Test",
      "when": "2020-08-28 20:11",
      "for": null,
      "accepted_by": 10,
      "name": "Sfgdsa",
      "mobile": "567896789",
      "email": "sdnsd@asd.com",
      "org_name": "Buii",
      "address": "Tata",
      "city": "Kolkata",
      "state": "West Bengal",
      "pincode": "789009",
      "country": "India",
      "latitude": null,
      "longitude": null,
      "client_ip": null,
      "device": null,
      "created_at": "2020-08-23T14:40:06.000000Z",
      "updated_at": "2020-09-02T18:07:40.000000Z"
    },
    "quote_details": null,
    "quote_amount": null,
    "isCalled": 0,
    "isChecked": 0,
    "isAccept": 1,
    "isGoing": 0,
    "checkIn": "2020-09-03 02:00:00",
    "checkOut": "2020-09-03 02:00:15",
    "pandit_id": 10,
    "pandit": {
      "id": 10,
      "name": "Ajay Roy",
      "mobile": "8013138886",
      "sms_token": null,
      "mobile_verified_at": null,
      "email": "ajay@abc.com",
      "email_token": null,
      "email_verified_at": null,
      "password": "5f4dcc3b5aa765d61d8327deb882cf99",
      "remember_token": null,
      "avatar": null,
      "address": "dum dum cantonment",
      "city": "Kolkata",
      "state": "West Bengal",
      "country": "India",
      "pincode": 700028,
      "location_id": null,
      "travelled": null,
      "adharcard": "34567890",
      "pancard": "ATD8994568",
      "specialisation": "Vastushastri",
      "services": [
        "Artrology",
        "Numerology"
      ],
      "min_charge": "500",
      "max_charge": "1000",
      "online_charge": "700",
      "qualification": "Test1",
      "experience": "12 Years",
      "language": "Bengali",
      "active": "0",
      "varify": "1",
      "payment_mode": "{\"bank_name\":\"HDFC Bank\",\"bank_address\":\"Dum Dum Cant\",\"ifsc_code\":\"HDFC098765\",\"account_no\":\"987654654678\",\"account_holder\":\"Abit Roy\"}",
      "created_at": "2020-08-31T19:03:56.000000Z",
      "updated_at": "2020-09-16T12:40:54.000000Z"
    },
    "visits": [
      {
        "id": 70,
        "pandit_activity_id": 22,
        "pandit_id": 10,
        "checkIn": "2020-09-03 02:00:00",
        "checkOut": null,
        "created_at": "2020-09-02T20:30:00.000000Z",
        "updated_at": "2020-09-02T20:30:00.000000Z"
      },
      {
        "id": 71,
        "pandit_activity_id": 22,
        "pandit_id": 10,
        "checkIn": null,
        "checkOut": "2020-09-03 02:00:15",
        "created_at": "2020-09-02T20:30:15.000000Z",
        "updated_at": "2020-09-02T20:30:15.000000Z"
      }
    ]
  }

铅模型

import 'dart:convert';

import 'package:PandeetApp/models/client_need.dart';
import 'package:PandeetApp/models/pandit.dart';
import 'package:PandeetApp/models/visit.dart';
import 'package:PandeetApp/services/lead.dart';

class Lead extends LeadService {
  int id;
  int clientNeedId;
  ClientNeed clientNeed;
  String quoteDetails;
  String quoteAmount;
  int isCalled;
  int isChecked;
  int isAccept;
  int isGoing;
  String checkIn;
  String checkOut;
  int panditId;
  Pandit pandit;
  List<Visit> visits;

  Lead({
    this.id,
    this.clientNeedId,
    this.clientNeed,
    this.quoteDetails,
    this.quoteAmount,
    this.isCalled,
    this.isChecked,
    this.isAccept,
    this.isGoing,
    this.checkIn,
    this.checkOut,
    this.panditId,
    this.pandit,
    this.visits,
  });

  Future<List<Lead>> fetchLeads() async {
    var datas = await super.fetchDatas();
    var dataList = json.decode(datas);

    var leads = dataList.map((e) => Lead.fromJson(e)).toList();
    return leads;
  }

  factory Lead.fromJson(Map<String, dynamic> json) {
    List<Visit> visits = List<Visit>();

    if (json['visits'] != null) {
      json['visits'].forEach((v) {
        visits.add(Visit.fromJson(v));
      });
    }

    return Lead(
      id: json['id'],
      clientNeedId: json['client_need_id'],
      clientNeed: json['clientNeed'] != null
          ? ClientNeed.fromJson(json['clientNeed'])
          : ClientNeed(),
      quoteDetails: json['quote_details'],
      quoteAmount: json['quote_amount'],
      isCalled: json['isCalled'],
      isChecked: json['isChecked'],
      isAccept: json['isAccept'],
      isGoing: json['isGoing'],
      checkIn: json['checkIn'],
      checkOut: json['checkOut'],
      panditId: json['pandit_id'],
      pandit:
          json['pandit'] != null ? Pandit.fromJson(json['pandit']) : Pandit(),
      visits: visits,
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    if (this.clientNeed != null) {
      data['clientNeed'] = this.clientNeed.toJson();
    }
    data['quote_details'] = this.quoteDetails;
    data['quote_amount'] = this.quoteAmount;
    data['isCalled'] = this.isCalled;
    data['isChecked'] = this.isChecked;
    data['isAccept'] = this.isAccept;
    data['isGoing'] = this.isGoing;
    data['checkIn'] = this.checkIn;
    data['checkOut'] = this.checkOut;
    if (this.pandit != null) {
      data['pandit'] = this.pandit.toJson();
    }
    if (this.visits != null) {
      data['visits'] = this.visits.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

屏幕部分

FutureBuilder<List<Lead>>(
      future: lead.fetchLeads(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          List leads = snapshot.data;
          // print(leads.length); // Data Not Getting Here
          if (leads.length > 0) {
            return dataList(leads);
          } else {
            return Container(
              padding: EdgeInsets.only(top: 200),
              child: Text("You have no clients"),
            );
          }
        } else if (snapshot.hasError) {
          print(snapshot.error);
          return Text("Server is Down now. Try again later");
        }

        return Container(
          padding: EdgeInsets.only(top: 200),
          child: CircularProgressIndicator(
            backgroundColor: Colors.redAccent,
          ),
        );
      },
    );

我想这里有问题。我已经完成了我的项目,但这种类型的问题毁了我的项目。我不明白我在这个问题上错在哪里。找不到确切的行号。给点提示,但确切的问题无法理解。请尽快帮助我。


共1个答案

匿名用户

不要将快照数据转换为List,而是使用var。尝试错误前的打印结果并继续

FutureBuilder<List<Lead>>(
      future: lead.fetchLeads(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          var leads = snapshot.data; // use var instead of List
           // try this in your case it is received as String
           print(leads.runtimeType.toString())
           // if it is string convert to List.
           var leadsJson = jsonDecode(leads)