![](/img/trans.png)
[英]Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>?' in type cast
[英]GetStorage | _CastError (type 'String' is not a subtype of type 'List<dynamic>?' in type cast)
我正在构建一个应用程序,用户可以在其中将收藏夹本地存储在他们的设备上。 我正在使用 GetStorage 来做到这一点。
这是我的 model class:
import 'package:cloud_firestore/cloud_firestore.dart';
class Product {
final String id;
final String name;
final double price;
final String image;
var rating;
Product({
required this.id,
required this.name,
required this.price,
required this.image,
required this.rating,
});
static Product fromSnapshot(DocumentSnapshot snap) {
Product product = Product(
id: snap.id,[enter image description here][1]
name: snap['name'],
price: snap['price'],
image: snap['image'],
rating: snap['rating'],
);
return product;
}
}
这就是我面临的问题
class ProductController extends GetxController {
TextEditingController searchTextController = TextEditingController();
var products = <Product>[].obs;
var favStorage = GetStorage();
var favorateList = <Product>[].obs;
var searchList = <Product>[].obs;
@override
void onInit() {
super.onInit();
List? storagedFaves = favStorage.read<List>('isFavoratesList');
if (storagedFaves != null) {
favorateList =
storagedFaves.map((e) => Product.fromSnapshot(e)).toList().obs ;
}
addProduct();
}
void addProduct() {
products.bindStream(FirebaseDB().getAllProducts());
}
void manageFavorates(String productId) async {
var existingIndex =
favorateList.indexWhere((element) => element.id == productId);
if (existingIndex >= 0) {
} else {
favorateList
.add(products.firstWhere((element) => element.id == productId));
}
await favStorage.write('isFavoratesList', favorateList);
}
bool isFavorates(String productId) {
return favorateList.any((element) => element.id == productId);
}
一切正常,但这部分
List? storagedFaves = favStorage.read<List>('isFavoratesList');
我知道问题出在哪里,并且我理解它发生的原因,但我仍然无法解决它
您从 GetStorage() 获得的数据类型是字符串而不是列表,您必须对其进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.