gpt4 book ai didi

firebase - 为什么 flutter 会为类内的 Firestore 方法返回错误,但在小部件内却很好?

转载 作者:行者123 更新时间:2023-12-03 03:57:10 26 4
gpt4 key购买 nike

所以我尝试创建一个类似于 rails、laravel 或其他框架的模型类,以便我可以更好地组织我的代码,我想添加实例方法来将数据保存到 firebase 这里是我的类。但是,由于某种原因,当我在我的事件类上调用 save 方法时。我收到此错误 MissingPluginException(在 channel plugins.flutter.io/cloud_firestore 上找不到方法 DocumentReference#setData 的实现)。

我尝试运行flutter clean,在谷歌搜索、升级flutter、卸载应用程序和运行packages get之后,没有任何效果。最后,我尝试在我的小部件组件中创建普通函数来创建一个新文档并添加数据。该功能运行良好,没有错误。另外,是的,我将云 Firestore 导入到我的事件类以及 geoflutterfire 中进行了适当的导入。为什么我会收到错误消息,有没有办法组织我的代码? firestore 方法是否不适用于不是从无状态小部件或 statfefull 小部件继承的小部件的 dart 文件?我尝试导入基础和 Material Dart 包,它们都被 Firestore 变灰,而我的 privateEvent 类显然没有使用它。

  final bool feedbackLocationOrActivity;
final String id;
final bool feedbackTimeAndDate;
final String name;
final String mediaUrl;
final String address;
final DateTime startTime;
final DateTime endTime;
List categories;
final String creatorId;
final String groupId;
final GeoFirePoint location;
List coHosts;
final String details;
final bool guestsCanInviteFriends;

PrivateEvent(
{this.feedbackLocationOrActivity,
this.id,
this.feedbackTimeAndDate,
this.name,
this.mediaUrl,
this.address,
this.startTime,
this.endTime,
this.categories,
this.creatorId,
this.location,
this.coHosts,
this.guestsCanInviteFriends,
this.details,
this.groupId});

factory PrivateEvent.fromDocument(DocumentSnapshot doc) {
return PrivateEvent(
id: doc['id'],
feedbackLocationOrActivity: doc['feedbackLocationOrActivity'],
feedbackTimeAndDate: doc['feedbackTimeAndDate'],
name: doc['name'],
mediaUrl: doc['mediaUrl'],
address: doc['address'],
startTime: doc['startTime'],
endTime: doc['endTime'],
categories: doc['categories'],
creatorId: doc['creatorId'],
groupId: doc['groupId'],
coHosts: doc['coHosts'],
guestsCanInviteFriends: doc['guestsCanInviteFriends'],
details: doc['details'],
location: doc['location']);
}

save() async {
return Firestore.instance
.collection('privateEvents')
.document(this.groupId)
.collection("events")
.add({
"id": this.id,
"feedbackLocationOrActivity": this.feedbackLocationOrActivity,
"feedbackTimeAndDate": this.feedbackTimeAndDate,
"name": this.name,
"mediaUrl": this.mediaUrl,
"address": this.address,
"startTime": this.startTime,
"endTime": this.endTime,
"categories": FieldValue.arrayUnion(this.categories),
"creatorId": this.creatorId,
"location": this.location.data,
"details": this.details,
"coHosts": FieldValue.arrayUnion(this.coHosts),
"guestsCanInviteFriends": this.guestsCanInviteFriends
}).then((response) {
response.updateData({"id": response.documentID});
return true;
}).catchError((onError) {
print("this is the error $onError");
return false;
});
}
}

The save method is the method that should save it to the database however it gets an error.

Here is where I call it.

createEvent({screenWidth, screenHeight, isLandscape}) async {
setState(() {
loading = true;
});
final String mediaUrl = await uploadImage(file);
if (mediaUrl.isEmpty || mediaUrl == null) {
setState(() {
loading = false;
});
eventTimeWarning(
screenWidth: screenWidth,
screenHeight: screenHeight,
isLandscape: isLandscape,
warningMessage:
"Your image didn't upload properly. There could be a problem with your internet connection. Please try again");
return;
}
PrivateEvent privateEvent = PrivateEvent(
feedbackTimeAndDate: feedbackTimeAndDate,
feedbackLocationOrActivity: feedbackLocationOrActivity,
name: eventName,
mediaUrl: mediaUrl,
address: eventAddress,
startTime: eventStartTimeAndDate,
endTime: eventEndTimeAndDate,
categories: selectedCategories(),
creatorId: Provider.of<UserData>(context).getUserId,
location: eventLocation,
guestsCanInviteFriends: guestsCanInviteFriends,
details: detailsController.text.trim(),
groupId:
Provider.of<PrivateGroupNormalData>(context).getGroupId);

final bool eventUploadSuccess = await sendEventInfo();
if (!eventUploadSuccess) {
setState(() {
eventTimeWarning(
screenWidth: screenWidth,
screenHeight: screenHeight,
isLandscape: isLandscape,
warningMessage:
"There was a problem creating your event. Please, check your internet connection and try again.");
loading = false;
});
return;
}
print("EVENT CREATED SUCCESSFULLY!");
}

As I said before. The above doesn't work. However On creating a function within the widget. The data is added to the database and there is no missingexception error.

Here is the working version.

Future<bool> sendEventInfo({String mediaUrl}) {
return Firestore.instance
.collection('privateEvents')
.document(Provider.of<PrivateGroupNormalData>(context).getGroupId)
.collection("events")
.add({
"feedbackLocationOrActivity": this.feedbackLocationOrActivity,
"feedbackTimeAndDate": this.feedbackTimeAndDate,
"name": this.eventName,
"mediaUrl": mediaUrl,
"address": eventAddress,
"startTime": eventStartTimeAndDate,
"endTime": eventEndTimeAndDate,
"categories": selectedCategories(),
"creatorId": Provider.of<UserData>(context).getUserId,
"location": eventLocation.data,
"details": detailsController.text.trim(),
"coHosts": FieldValue.arrayUnion(),
"guestsCanInviteFriends": this.guestsCanInviteFriends
}).then((response) {
response.updateData({"id": response.documentID});
return true;
}).catchError((onError) {
print("this is the error $onError");
return false;
});
}

All I do is replace the call to instance method with the call to the above function and now it works fine without the missing plugin error.

最佳答案

答案是我必须将我的 Firestore 实例设为静态实例才能正确访问它。

关于firebase - 为什么 flutter 会为类内的 Firestore 方法返回错误,但在小部件内却很好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60144252/

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