List 未处理的异常:类型';列表<;动态>';不是类型为';列表<;字符串>';:颤振

List 未处理的异常:类型';列表<;动态>';不是类型为';列表<;字符串>';:颤振,list,firebase,flutter,types,List,Firebase,Flutter,Types,//行中有错误:_picture=snapshot.data()[picture],它正在返回未处理的异常。“列表”类型不是“列表”类型的子类型。 import'包:cloud_firestore/cloud_firestore.dart'; 类产品模型{ 静态常量ID=“ID”; 静态常量NAME=“NAME”; 静态常量PICTURE=“PICTURE”; 静态常量价格=“价格”; 静态常量DESCRIPTION=“DESCRIPTION”; 静态常量CATEGORY=“CATEGORY”;

//行中有错误:_picture=snapshot.data()[picture],它正在返回未处理的异常。“列表”类型不是“列表”类型的子类型。

import'包:cloud_firestore/cloud_firestore.dart';
类产品模型{
静态常量ID=“ID”;
静态常量NAME=“NAME”;
静态常量PICTURE=“PICTURE”;
静态常量价格=“价格”;
静态常量DESCRIPTION=“DESCRIPTION”;
静态常量CATEGORY=“CATEGORY”;
静态常量数量=“数量”;
静态const BRAND=“品牌”;
静态常量PAYSTACK_ID=“paystackId”;
字符串_id;
字符串\u名称;
列表-图片;
字符串描述;
字符串_类;
弦乐品牌;
国际单位数量;
国际价格;
字符串_paystackId;
字符串get id=>\u id;
字符串get name=>\u name;
列表获取图片=>\u图片;
字符串get brand=>\u brand;
字符串get category=>\u category;
字符串get description=>\u description;
int get quantity=>\u quantity;
int get price=>\u price;
字符串get paystackId=>\u paystackId;
ProductModel.fromSnapshot(DocumentSnapshot快照){
_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))两者都在引发未处理的异常类型“List”不是“List”类型的子类型

   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;
});
 }
 }
import'包:cloud_firestore/cloud_firestore.dart';
进口“包装:farmers_电子商务/模型/产品.dart”;
类别产品服务{
String collection=“products”;
FirebaseFirestore _firestore=FirebaseFirestore.instance;
Future getProducts()异步{
QuerySnapshot结果=等待_firestore.collection(collection.get();
列出产品=[];
for(结果.docs中的DocumentSnapshot产品){
products.add(ProductModel.fromSnapshot(product));
}
退货产品;
}
未来的searchProducts({String productName}){
//将第一个字符转换为大写的代码
字符串searchKey=productName[0]。toUpperCase()+productName.substring(1);
返回火场
.收集(收集)
.orderBy(“名称”)
.startAt([searchKey])
.endAt([searchKey+'\uf8ff']))
.get()
.然后((结果){
列出产品=[];
for(结果.docs中的DocumentSnapshot产品){
products.add(ProductModel.fromSnapshot(product));
}
退货产品;
});
}
}

当我将
List
更改为
List
时,它起作用了。基本上,这可以自动扫描与“图片”集合关联的不同图像。

您可以将您的JSON结构添加到问题中吗?您还可以在中发布产品的屏幕截图吗firestore@fartem我的项目中没有Json结构。@当然可以。我刚把它贴出来。检查页面顶部。是否可以调试应用程序并打印
snapshot.data()[图片]
输出?
   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;
});
 }
 }