dart 如何在Flutter中获取、设置、更新和删除云存储中的数据?

mrzz3bfm  于 2023-01-18  发布在  Flutter
关注(0)|答案(4)|浏览(139)

我尝试了一些代码,但得到一个异常。
我得到的异常:java.lang.IllegalArgumentException: Invalid document reference. Document references must have an even number of segments, but Users has 1
我搜索了它,根据this,文档引用必须具有偶数个段**,如:收款-单据-收款-单据-收款-单据**

从firestore获取数据的查询:

String getIsNewUSer;
Firestore.instance.collection('Users').document(uid).get().then((DocumentSnapshot document){
       print("document_build:$document");
        setState(() {
         getIsNewUSer=document['IsNewUser'];
         print("getIsNewUSe:$getIsNewUSer");
        });
             
    });

查询将数据更新到firestore:

Firestore.instance
            .collection('Users')
            .document(uid)
            .updateData({
              "IsNewUser":"1"
            }).then((result){
              print("new USer true");
            }).catchError((onError){
             print("onError");
            });

这些代码行在我得到以上异常.

初始状态:

void initState()  {
    super.initState();
    this.uid = '';

FirebaseAuth.instance.currentUser().then((val){
      setState(() {
      this.uid= val.uid; 
       print("uid_init: $uid");
      });
   });

}
s4n0splo

s4n0splo1#

空安全代码:

      • 获取数据:**
var collection = FirebaseFirestore.instance.collection('collection');
var docSnapshot = await collection.doc('doc_id').get();
Map<String, dynamic>? data = docSnapshot.data();
      • 设置数据:**
var collection = FirebaseFirestore.instance.collection('collection');
collection.add(someData);
      • 更新数据**:
var collection = FirebaseFirestore.instance.collection('collection');
collection 
    .doc('foo_id') // <-- Doc ID where data should be updated.
    .update(newData);
      • 删除数据:**
var collection = FirebaseFirestore.instance.collection('collection');
collection 
    .doc('some_id') // <-- Doc ID to be deleted. 
    .delete();
t5zmwmid

t5zmwmid2#

在查询中替换此部分:

Firestore.instance.collection('Users').document(uid)

Firestore.instance.document('Users/$uid')

收款-单据-收款-单据-收款-单据
基本上你已经有答案了。

00jrzges

00jrzges3#

1.获取数据
final docRef = FirebaseFirestore.instance.collection("cities").doc("SF");
docRef.get().then(
  (DocumentSnapshot doc) {
    final data = doc.data() as Map<String, dynamic>;
    // ...
  },
  onError: (e) => print("Error getting document: $e"),
);

另请参阅:How to use StreamBuilder and FutureBuilder for single and multiple documents

2.添加数据
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"));

另请参阅:How to add documents with custom and generated Id

3.更新数据
final washingtonRef = FirebaseFirestore.instance.collection("cites").doc("DC");
washingtonRef.update({"capital": true}).then(
    (value) => print("DocumentSnapshot successfully updated!"),
    onError: (e) => print("Error updating document $e"));

另请参阅:Different ways to update document

4.删除数据
FirebaseFirestore.instance.collection("cities").doc("DC").delete().then(
      (doc) => print("Document deleted"),
      onError: (e) => print("Error updating document $e"),
    );
qcbq4gxm

qcbq4gxm4#

有可能FirebaseAuth.instance.currentUser() future没有完成并填充this.uid。因此this.uid == ''(空字符串)。因此Firestore在您尝试更新Users(一个集合)处的文档时抛出错误。
您可以通过在update语句之前打印this.uid来验证这一点。
一种方法是使用helper方法更新

Future<void> update(Map data) async {
    final user = await FirebaseAuth.instance.currentUser();
    return Firestore.instance.collection('Users').document(user.uid).updateData(data);
}

然后可以使用helpr方法update({isNewUser: "1"}).then((r) {...})....
您也可以按照相同的方法获取文档。

相关问题