首页 > 解决方案 > 未处理的异常:键入“列表”' 不是类型 '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;
});
 }
 }

标签: listfirebasefluttertypes

解决方案


当我更改List <String>List<dynamic>. 基本上,这可以自动扫描与“图片”集合相关的不同图像。


推荐阅读