gpt4 book ai didi

firebase - Dart 未处理异常 : type 'List' is not a subtype of type 'Iterable>' in type cast

转载 作者:行者123 更新时间:2023-12-05 04:38:34 25 4
gpt4 key购买 nike

我正在开发一个 flutter 应用程序,当我将它升级到 Flutter 2.0 时,一个新错误开始出现。这是关于类型转换的错误,但我不确定为什么会发生这种情况,因为旧版本没有问题。错误如下:

Dart Unhandled Exception: type 'List<dynamic>' is not a subtype of 

type 'Iterable<Map<dynamic, dynamic>>' in type cast, stack trace: #0 UserModel._convertCartItems (package:test_app/model/user.dart:67:31)
E/flutter (30545): #1 new UserModel.fromSnapshot (package:test_app/model/user.dart:58:12)
E/flutter (30545): #2 UserServices.getUserById.<anonymous closure> (package:test_app/services/user.dart:33:26)
E/flutter (30545): <asynchronous suspension>
E/flutter (30545): #3 UserProvider._onStateChanged (package:test_app/provider/user.dart:125:20)
E/flutter (30545): <asynchronous suspension>
E/flutter (30545):

与之关联的代码是:

    List<CartItemModel> _convertCartItems(List cart) {
List<CartItemModel> convertedCart = [];
for (Map cartItem in cart as Iterable<Map<dynamic, dynamic>>) {
convertedCart.add(CartItemModel.fromMap(cartItem));
}
return convertedCart;
}

我试图删除 as Iterable<>但随后该应用程序只渲染了一次并开始了新的错误。

最佳答案

您的 cart 变量是一个列表,而不是一个映射。您需要将迭代过程更改为:

for (final rawCartItem in cart) {
convertedCart.add(CartItemModel.fromMap(rawCartItem));
}

关于firebase - Dart 未处理异常 : type 'List<dynamic>' is not a subtype of type 'Iterable<Map<dynamic, dynamic>>' in type cast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70577193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com