_TypeError(类型'List<dynamic>'不是'Map<String,dynamic>'类型的子类型)
Posted
技术标签:
【中文标题】_TypeError(类型\'List<dynamic>\'不是\'Map<String,dynamic>\'类型的子类型)【英文标题】:_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>') in flutter_TypeError(类型'List<dynamic>'不是'Map<String,dynamic>'类型的子类型) 【发布时间】:2021-09-12 06:46:30 【问题描述】:您好,我正在进行 api 调用,我正在获取横幅数组。我遇到了错误-
_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>')
banners_bloc.dart
class BannersBloc
late BannerRepository _bannerRepository;
late StreamController _streamController;
StreamSink get bannerListSink => _streamController.sink;
BannersBloc()
_streamController = StreamController();
_bannerRepository = BannerRepository();
fetchBanners();
fetchBanners() async
bannerListSink.add('Fetching Banners');
try
Banners banners = await _bannerRepository.getBanners();
bannerListSink.add(banners);
catch (e)
bannerListSink.add(e.toString());
print(e);
dispose()
_streamController.close();
banners.dart
class Banners
late String bannerImageUrl;
late String bannerImageAlt;
late bool isActive;
late int order;
late String id;
Banners(
required this.bannerImageUrl,
required this.bannerImageAlt,
required this.isActive,
required this.order,
required this.id);
Banners.fromJson(Map<String, dynamic> json)
bannerImageUrl = json['bannerImageUrl'];
bannerImageAlt = json['bannerImageAlt'];
isActive = json['isActive'];
order = json['order'];
id = json['id'];
Map<String, dynamic> toJson()
final Map<String, dynamic> data = new Map<String, dynamic>();
data['bannerImageUrl'] = this.bannerImageUrl;
data['bannerImageAlt'] = this.bannerImageAlt;
data['isActive'] = this.isActive;
data['order'] = this.order;
data['id'] = this.id;
return data;
api_base_helper.dart
class APIHelper
final String _baseURL =
"https://raw.githubusercontent.com/gautam-in/shopping-cart-assignment/master/server/";
Future<dynamic> get(String url) async
try
final response = await http.get(Uri.parse(_baseURL + url));
return json.decode(response.body);
catch (err)
throw new Exception("Something went wrong");
get_banners.dart
class BannerRepository
Future getBanners() async
final response = await new APIHelper().get("banners/index.get.json");
return Banners.fromJson(response);
【问题讨论】:
这能回答你的问题吗? Error: List<dynamic> is not a subtype of type Map<String, dynamic> 【参考方案1】:如果您的 API 响应是横幅列表,则错误是由 getBanners
引发的。因为fromJson
方法需要Map<String, dynamic>
类型。要解决此问题,您需要遍历响应并为响应列表中的每个项目调用 Banners.fromJson
。
class BannerRepository
Future<List<Banners>> getBanners() async
final response = await new APIHelper().get("banners/index.get.json");
// Sample Code, handler errors and other stuff
return response.map((e) =>Banners.fromJson(e)).toList();
【讨论】:
您的错误来自哪里?粘贴完整的错误,以便我们可以正确追踪错误 这一行返回 response.map((e) => Banners.fromJson(response)).toList();颤振:类型 'Liste
而不是response
。更新了代码块
现在迭代此地图后,将流向 fromJson 中的 pojo 类,然后在迭代后给出相同的错误。
更新您的问题,包括确切的行号和发生错误的文件。以上是关于_TypeError(类型'List<dynamic>'不是'Map<String,dynamic>'类型的子类型)的主要内容,如果未能解决你的问题,请参考以下文章
Flutter json解码_TypeError(类型'List <dynamic>'不是'Map <dynamic,dynamic>'类型的子类型)[关闭]
TypeError: %: 'list' 和 'int' 不支持的操作数类型 |很有趣的案例?
Python debug——TypeError unhashable type(list/set/dict)
Python_异常:TypeError: write() argument must be str, not list
_TypeError(类型 '_InternalLinkedHashMap<String, dynamic>' 不是类型 'Iterable<dynamic>' 的子类型) F