带有点的 Dart 控制台应用程序错误'List<dynamic> is not a subtype of type Map<String, dynamic>'



我有这个问题,我想使用一个端点,但我有这个错误,我只是想练习飞镖,我是飞镖世界的新手:),我试图用类似的问题来解决这个问题,但我不能。

我有这个问题:

'List'不是类型'Map<String,>'的子类型这是一个控制台应用程序这是我的代码:

import 'package:http/http.dart' as http;
import 'package:marchex_api/clases/business.dart';
void getMarchex(){
final url = Uri.parse('https:#');
http.get(url).then((res){
final servicios =  Business.fromJson(res.body); 
//print(servicios);
}).catchError((error)=>print(error));
}

这是类:


class Business {
Business({
required this.id,
required this.businessName,
required this.businessNumber,
required this.billableDuration,
required this.publisherPayout,
required this.businessLatitude,
required this.businessLongitude,
required this.callTrackingNumber,
required this.businessAddress1,
required this.businessCity,
required this.businessState,
required this.businessZip,
required this.postbackUrl,
required this.businessShortDescription,
required this.businessLongDescription,
});
String id;
String businessName;
String businessNumber;
String billableDuration;
String publisherPayout;
String businessLatitude;
String businessLongitude;
String callTrackingNumber;
String businessAddress1;
String businessCity;
String businessState;
String businessZip;
String postbackUrl;
String businessShortDescription;
String businessLongDescription;
factory Business.fromJson(String str) => Business.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Business.fromMap(Map<String, dynamic> json) => Business(
id: json["id"],
businessName: json["businessName"],
businessNumber: json["businessNumber"] == null ? null : json["businessNumber"],
billableDuration: json["billableDuration"],
publisherPayout: json["publisherPayout"],
businessLatitude: json["businessLatitude"] == null ? null : json["businessLatitude"],
businessLongitude: json["businessLongitude"] == null ? null : json["businessLongitude"],
callTrackingNumber: json["callTrackingNumber"],
businessAddress1: json["businessAddress1"] == null ? null : json["businessAddress1"],
businessCity: json["businessCity"] == null ? null : json["businessCity"],
businessState: json["businessState"] == null ? null : json["businessState"],
businessZip: json["businessZip"] == null ? null : json["businessZip"],
postbackUrl: json["postbackUrl"],
businessShortDescription: json["businessShortDescription"] == null ? null : json["businessShortDescription"],
businessLongDescription: json["businessLongDescription"] == null ? null : json["businessLongDescription"],
);
Map<String, dynamic> toMap() => {
"id": id,
"businessName": businessName,
"businessNumber": businessNumber == null ? null : businessNumber,
"billableDuration": billableDuration,
"publisherPayout": publisherPayout,
"businessLatitude": businessLatitude == null ? null : businessLatitude,
"businessLongitude": businessLongitude == null ? null : businessLongitude,
"callTrackingNumber": callTrackingNumber,
"businessAddress1": businessAddress1 == null ? null : businessAddress1,
"businessCity": businessCity == null ? null : businessCity,
"businessState": businessState == null ? null : businessState,
"businessZip": businessZip == null ? null : businessZip,
"postbackUrl": postbackUrl,
"businessShortDescription": businessShortDescription == null ? null : businessShortDescription,
"businessLongDescription": businessLongDescription == null ? null : businessLongDescription,
};
}

您的API返回一个业务条目列表,因此您将希望以List<Business>结束,但在这行final servicios = Business.fromJson(res.body)中,您试图将整个列表解析为单个业务。循环遍历已解码的List<dynamic>,将每个元素解析为业务,如下所示:

final url = Uri.parse(
'https://services.marchex.com/mcm/listings?partnerId=bchybs0z&searchType=keywordOrBusiness&searchTerm=Attorney&zip=30334&limit=20&radius=40',
);
final response = await http.get(url);
final j = json.decode(response.body) as List<dynamic>;
final businesses = j
.map<Business>((e) => Business.fromMap(e as Map<String, dynamic>))
.toList();

顺便说一句,你的命名构造函数的代码过于复杂,很可能是错误的。您正在测试null,然后尝试将null分配给String而不是String?。如果使用StringString?,更倾向于使用??运算符并提供虚拟值:

class Business {
Business({
required this.id,
required this.businessName,
required this.businessNumber,
required this.billableDuration,
required this.publisherPayout,
required this.businessLatitude,
required this.businessLongitude,
required this.callTrackingNumber,
required this.businessAddress1,
required this.businessCity,
required this.businessState,
required this.businessZip,
required this.postbackUrl,
required this.businessShortDescription,
required this.businessLongDescription,
});
String id;
String businessName;
String businessNumber;
String billableDuration;
String publisherPayout;
String businessLatitude;
String businessLongitude;
String callTrackingNumber;
String businessAddress1;
String businessCity;
String businessState;
String businessZip;
String postbackUrl;
String businessShortDescription;
String businessLongDescription;
factory Business.fromMap(Map<String, dynamic> json) => Business(
id: json['id'],
businessName: json['businessName'],
businessNumber: json['businessNumber'] ?? '',
billableDuration: json['billableDuration'],
publisherPayout: json['publisherPayout'],
businessLatitude: json['businessLatitude'] ?? '',
businessLongitude: json['businessLongitude'] ?? '',
callTrackingNumber: json['callTrackingNumber'],
businessAddress1: json['businessAddress1'] ?? '',
businessCity: json['businessCity'] ?? '',
businessState: json['businessState'] ?? '',
businessZip: json['businessZip'] ?? '',
postbackUrl: json['postbackUrl'],
businessShortDescription: json['businessShortDescription'] ?? '',
businessLongDescription: json['businessLongDescription'] ?? '',
);
}

相关内容

  • 没有找到相关文章

最新更新