[英]How to create document in collection in FireStore with Flutter
Collection has default permissions on Firebase Console.集合在 Firebase 控制台上有默认权限。
I sign in my user correctly with email and password.我使用 email 和密码正确登录我的用户。
user = await _auth.signInWithEmailAndPassword( email: "email@gmail.com", password: "password");
Then after uploading image successfully to FireStorage, I try to run a transaction as well to update the document.然后在将图像成功上传到 FireStorage 后,我尝试运行一个事务来更新文档。
var fileName = _textController.text.toLowerCase();
StorageUploadTask putFile =
storage.ref().child("region/$fileName").putFile(_regionImage);
UploadTaskSnapshot uploadSnapshot = await putFile.future;
var regionData = new Map();
regionData["label"] = _textController.text;
var pictureData = new Map();
pictureData["url"] = uploadSnapshot.downloadUrl.toString();
pictureData["storage"] = "gs://app-db.appspot.com/region/$fileName";
regionData["picture"] = pictureData;
DocumentReference currentRegion =
Firestore.instance.collection("region").document(fileName);
Firestore.instance.runTransaction((transaction) async {
DocumentSnapshot freshSnap = await transaction.get(currentRegion);
print(freshSnap.exists);
//await transaction.set(freshSnap.reference, regionData);
await transaction.set(currentRegion, regionData);
print("instance created");
});
I get this error when trying to run a transaction.尝试运行事务时出现此错误。
It is the same If I try to set to freshSnap.reference
or directly to currentRegion
.如果我尝试设置为
freshSnap.reference
或直接设置为currentRegion
,这是相同的。 https://gist.github.com/matejthetree/f2a57c929d01919bd46da8ca6d5b6fb1 https://gist.github.com/matejthetree/f2a57c929d01919bd46da8ca6d5b6fb1
Note that at line 15 error for transaction starts, but before I get no auth token error as well for FireStorage although I successfully upload and download images to storage.请注意,在第 15 行交易开始时出现错误,但在我没有收到 FireStorage 的身份验证令牌错误之前,尽管我已成功将图像上传和下载到存储。
How should I approach document creation in FireStore我应该如何处理 FireStore 中的文档创建
Seems like the problem was in the way I created the map.似乎问题出在我创建地图的方式上。
Map<String, dynamic> regionData = new Map<String, dynamic>();
regionData["label"] = _textController.text;
Map<String, dynamic> pictureData = new Map<String, dynamic>();
pictureData["url"] = uploadSnapshot.downloadUrl.toString();
pictureData["storage"] = "gs://app-db.appspot.com/region/$fileName";
regionData["picture"] = pictureData;
DocumentReference currentRegion =
Firestore.instance.collection("region").document(fileName);
Firestore.instance.runTransaction((transaction) async {
await transaction.set(currentRegion, regionData);
print("instance created");
});
this code works now此代码现在有效
Firestore
is replaced with FirebaseFirestore
and a lot of API has also been updated. Firestore
被FirebaseFirestore
取代,很多 API 也得到了更新。
var myData = {'foo': 0, 'bar': true};
var collection = FirebaseFirestore.instance.collection('collection');
collection
.add(myData) // <-- Your data
.then((_) => print('Added'))
.catchError((error) => print('Add failed: $error'));
There are 2 ways:有2种方式:
Use set()
method使用
set()
方法
final city = <String, String>{
"name": "Los Angeles",
"state": "CA",
"country": "USA"
};
FirebaseFirestore.instance
.collection("cities")
.doc("LA")
.set(city)
.onError((e, _) => print("Error writing document: $e"));
Use add()
method使用
add()
方法
final data = {"name": "Tokyo", "country": "Japan"};
FirebaseFirestore.instance
.collection("cities")
.add(data).then((documentSnapshot) =>
print("Added Data with ID: ${documentSnapshot.id}"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.