gpt4 book ai didi

flutter) 类型 'List' 不是类型 'List' 的子类型

转载 作者:行者123 更新时间:2023-12-05 01:51:18 24 4
gpt4 key购买 nike

我想在使用 firebase 电子邮件身份验证完成注册后获取用户模型。但是,当我尝试从 firestore 获取用户模型时,会打印以下错误:

type 'List<dynamic>' is not a subtype of type 'List<String>'

我的用户模型如下

class UserModel {
late String userKey;
late String nickname;
late String email;
late List<String> listItem; // error!!
late GeoFirePoint geoFirePoint;
late bool isPostOn;
late bool isLogin;
late DateTime createdDate;

DocumentReference? reference;

UserModel({
....
});

UserModel.fromJson(Map<String, dynamic> json, this.userKey) // this.reference
: nickname = json[DOC_NICKNAME],
....
createdDate = json[DOC_CREATEDDATE] == null
? DateTime.now().toUtc()
: (json[DOC_CREATEDDATE] as Timestamp).toDate();

UserModel.fromSnapshot(DocumentSnapshot<Map<String, dynamic>> snapshot)
: this.fromJson(snapshot.data()!, snapshot.id);

Map<String, dynamic> toJson() {
var map = <String, dynamic>{};
map[DOC_NICKNAME] = nickname;
....
return map;
}
}

这是从 Firestore 获取用户模型。//类用户服务

  Future<UserModel> getUserModel(String userKey) async {
DocumentReference<Map<String, dynamic>> documentReference =
FirebaseFirestore.instance.collection(COL_USERS).doc(userKey);

final DocumentSnapshot<Map<String, dynamic>> documentSnapshot =
await documentReference.get();

UserModel userModel = UserModel.fromSnapshot(documentSnapshot);
return userModel;
}

//用户提供者

  Future _setNewUser(User? user) async {
if (user != null && user.email != null) {
....
// ! Error
// test 1
// List<String> subjectList = prefs.getStringList(SHARED_SUBJECTS) ?? [];

// test 2
List<String> listItem =
prefs.getStringList(SHARED_ITEM)?.cast<String>() ??
[].cast<String>();

String email = user.email!;
String userKey = user.uid;

UserModel userModel = UserModel(
userKey: "",
listItem: listItem, // error
....
);

// create model and firestore upload
await UserService().createNewUser(userModel.toJson(), userKey);

// get user model, error!!
try {
_userModel = await UserService().getUserModel(userKey);
} catch (e) {
logger.w("get model error: $e");
}
}
}

我应该如何处理列表数据来解决这个问题?谢谢!

最佳答案

你必须转换列表:

listItem = List<String>.from(data['listItem'])

关于flutter) 类型 'List<dynamic>' 不是类型 'List<String>' 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72375404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com