gpt4 book ai didi

flutter - 删除 firestore 表中的 map

转载 作者:行者123 更新时间:2023-12-02 16:11:46 25 4
gpt4 key购买 nike

我在删除 Firestore 数据表中的 map 时遇到问题。实际上,我要么删除整个数组,要么收到以下类型的错误:

flutter: Failed to delete 1: Invalid argument: Instance of '_CompactLinkedHashSet '

我附上我的类(class),以便您更好地理解。提前谢谢您

类删除_描述:

import 'package:cloud_firestore/cloud_firestore.dart';

class DeleteDescription {
final String city;
final String citee;
final int value;
CollectionReference cities = FirebaseFirestore.instance.collection('city');

DeleteDescription(this.city, this.citee, this.value) {
deleteDescription();
}

Future<void> deleteDescription() {
return cities
.doc(city)
.collection("citee")
.doc(citee)
.set({
"Description": FieldValue.arrayRemove([
{0}
])
})
.then((value) => print("$citee Deleted"))
.catchError((error) => print("Failed to delete $value: $error"));
}
}

类(class)重述:

import 'package:ampc_93/fonction/firebase_crud/delete_description.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class ReadDescription extends StatefulWidget {
final String titreCity;
final String titreCitee;

ReadDescription(this.titreCity, this.titreCitee);

@override
_ReadDescriptionState createState() => _ReadDescriptionState();
}

class _ReadDescriptionState extends State<ReadDescription> {
@override
Widget build(BuildContext context) {
CollectionReference cities = FirebaseFirestore.instance.collection("city");
return FutureBuilder<DocumentSnapshot>(
future: cities
.doc(widget.titreCity)
.collection("citee")
.doc(widget.titreCitee)
.get(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Documents does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
var data = snapshot.data!.data() as Map<String, dynamic>;
if (data["Description"] == null) {
return Text("");
} else {
return ListView.separated(
itemBuilder: (context, index) {
return ListTile(
title: Text(
data["Description"][index]["Identite"],
textAlign: TextAlign.justify,
),
subtitle: Text(
data["Description"][index]["Role"],
textAlign: TextAlign.justify,
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.red),
),
leading: Icon(Icons.person),
trailing: IconButton(
onPressed: () => DeleteDescription(
widget.titreCity, widget.titreCitee, index),
icon: Icon(Icons.delete_forever),
color: Colors.red[300],
));
},
separatorBuilder: (context, index) => Divider(),
itemCount: data["Description"].length);
}
}
return Text("Loading");
},
);
}
}

我指定在我的数据库中,“Description”是一个数组,因此我想删除例如“Description”编号为 0 的所有元素

FIRESTORE DATABASE

最佳答案

您正在使用的 FieldValue.arrayRemove 无法以这种方式工作。从 firestore 列表中删除数据有两种方法。第一种方法是传递要删除的 FieldValue.arrayRemove 中的元素(不是索引)。第二种方法是从 firestore 获取集合并根据需要修改数据并更新 firestore 中的集合。查看下面的代码以获得更多理解。

import 'package:cloud_firestore/cloud_firestore.dart';

class DeleteDescription {
final String city;
final String citee;
final int value;
CollectionReference cities = FirebaseFirestore.instance.collection('city');

DeleteDescription(this.city, this.citee, this.value) {
deleteDescription();
}

Future<void> deleteDescription() {
final snapshot = await cities.doc(city).collection("citee").doc(citee).get();

/* Get list from firestore */
final list = snapshot["Description"] as List;

/* Remove first or any element and delete from list */
list.removeAt(0);

/* Update same list in firestore*/
await cities
.doc(city)
.collection("citee")
.doc(citee)
.set({"Description": list}).then((value) => print(" Deleted"));


}
}

关于flutter - 删除 firestore 表中的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67801944/

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