Henry Ogar
Henry Ogar

Reputation: 21

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

// i am having error in the line: _picture = snapshot.data()[PICTURE]; , it is returning the unhandled exception. type 'List' is not a subtype of type 'List' This is the image of my 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] ;

}
}

//Also the second error detected is coming from line: products.add(ProductModel.fromSnapshot(product)); both are throwing unhandled exception type 'List' is not a subtype of type '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;
});
 }
 }

Upvotes: 1

Views: 90

Answers (1)

Henry Ogar
Henry Ogar

Reputation: 21

It worked when i changed List <String> to List<dynamic>. Basically, this enabled to automatically scan through the different images associated to the 'picture' collection.

Upvotes: 1

Related Questions