list - 未处理的异常:键入“列表”' 不是类型 'List 的子类型':颤动
问题描述
// 我在行中有错误:_picture = snapshot.data()[PICTURE]; ,它正在返回未处理的异常。“List”类型不是“List”类型的子类型 这是我的 Firebase 的图像
import 'package:cloud_firestore/cloud_firestore.dart';
class ProductModel {
static const ID = "id";
static const NAME = "name";
static const PICTURE = "picture";
static const PRICE = "price";
static const DESCRIPTION = "description";
static const CATEGORY = "category";
static const QUANTITY = "quantity";
static const BRAND = "brand";
static const PAYSTACK_ID = "paystackId";
String _id;
String _name;
List<String> _picture;
String _description;
String _category;
String _brand;
int _quantity;
int _price;
String _paystackId;
String get id => _id;
String get name => _name;
List<String> get picture => _picture;
String get brand => _brand;
String get category => _category;
String get description => _description;
int get quantity => _quantity;
int get price => _price;
String get paystackId => _paystackId;
ProductModel.fromSnapshot(DocumentSnapshot snapshot) {
_id = snapshot.data()[ID];
_brand = snapshot.data()[BRAND];
_description = snapshot.data()[DESCRIPTION] ?? " ";
_price = snapshot.data()[PRICE].floor();
_category = snapshot.data()[CATEGORY];
_name = snapshot.data()[NAME];
_picture = snapshot.data()[PICTURE];
_paystackId = snapshot.data()[PAYSTACK_ID] ;
}
}
//另外检测到的第二个错误来自以下行:products.add(ProductModel.fromSnapshot(product)); 两者都抛出未处理的异常类型“列表”不是“列表”类型的子类型
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:farmers_ecommerce/models/product.dart';
class ProductServices {
String collection = "products";
FirebaseFirestore _firestore = FirebaseFirestore.instance;
Future<List<ProductModel>> getProducts() async {
QuerySnapshot result= await _firestore.collection(collection).get();
List<ProductModel> products = [];
for (DocumentSnapshot product in result.docs) {
products.add(ProductModel.fromSnapshot(product));
}
return products;
}
Future<List<ProductModel>> searchProducts({String productName}) {
// code to convert the first character to uppercase
String searchKey = productName[0].toUpperCase() + productName.substring(1);
return _firestore
.collection(collection)
.orderBy("name")
.startAt([searchKey])
.endAt([searchKey + '\uf8ff'])
.get()
.then((result) {
List<ProductModel> products = [];
for (DocumentSnapshot product in result.docs) {
products.add(ProductModel.fromSnapshot(product));
}
return products;
});
}
}
解决方案
当我更改List <String>
为List<dynamic>
. 基本上,这可以自动扫描与“图片”集合相关的不同图像。
推荐阅读
- bash - Bash 脚本在循环后终止
- python-3.x - lxml 和不正确的源编码
- json - Flutter:如何将列表转换为 JSON
- python - 减少 Python 静态库的大小
- python - pytorch中的张量是否有类似df.mask之类的功能?
- javascript - coinbase oauth 授权返回 html,如何与我的网络应用程序集成
- vb.net - AudioPlayMode.BackgroundLoop 在大约 3 小时后停止
- soap - Karate - Conditional logic with string and array
- javascript - forEach 不会像 for 循环那样向外部函数返回值
- hololens - MRTK2 的 IMixedRealityPointerHandler 的一致性