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



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

我尝试在谷歌搜索、升级 Flutter、卸载应用程序并运行包 get 之后干净地运行 Flutter,但没有任何效果。最后,我尝试在我的小部件组件中创建普通函数来创建新文档并添加数据。该函数工作正常,没有错误。另外,是的,我做了适当的导入,将云火存储导入到我的事件类中,以及Geoflutterfire。为什么我会收到错误,有没有办法组织我的代码?Firestore 方法是否不适用于不是从无状态小部件或 statfefull 小部件继承的小部件的飞镖文件?我尝试导入基础和材质飞镖包,它们在 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 实例成为静态实例才能正确访问它。

最新更新