gpt4 book ai didi

firebase - Flutter/Firebase-文档 ID 未知时如何执行删除文档功能?

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

我是 Flutter 和开发应用程序的新手,我在其中显示 ListView 中的数据。里面有一个删除图标按钮。我希望应该删除选定的 ListTile 数据。它以List的形式显示数据但不删除。
在我添加了 deleteData() 方法之后。它显示以下错误:

'package:flutter/src/rendering/silver_multi_box_adaptor.dart':Failed assertion: line 263 pos 16: 'child == null || indexOf(child) > index' : is not true.



任何帮助都受到高度赞赏。这是我到目前为止所做的:
class OrderRequestModel extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: UserOrderRequestModel(),
theme: new ThemeData(
primarySwatch: Colors.red,
),
);
}
}

class UserOrderRequestModel extends StatefulWidget {
@override
_UserOrderRequestModelState createState() {
return _UserOrderRequestModelState();
}
}

class _UserOrderRequestModelState extends State<UserOrderRequestModel> {
deleteData(docId) {
Firestore.instance
.collection('order_tbl')
.document(docId)
.delete()
.catchError((e) {
print(e);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _buildBody(context),
);
}
Stream<QuerySnapshot> getData() async*{
FirebaseUser firebaseUser = await FirebaseAuth.instance.currentUser();
yield* Firestore.instance.collection('order_tbl').orderBy('timestamp', descending: true).snapshots();
}

Widget _buildBody(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: getData(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildList(context, snapshot.data.documents);
},
);
}

Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
return ListView(
padding: const EdgeInsets.only(top: 5.0),
children: snapshot.map((data) => _buildListItem(context, data)).toList(),
);
}

Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
final record = Record.fromSnapshot(data);
String _string =getTimeDifferenceFromNow(record.timestamp.toDate());
return Padding(
key: ValueKey(record.animal),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: new ListTile(
title: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(
"${record.quantity} kg ${record.animal} - ${record.mobile}",
style: new TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
),
new Text(
_string,
style: new TextStyle(color: Colors.grey, fontSize: 12.0),
),
],
),
trailing: IconButton(
icon: Icon(Icons.delete_forever),
onPressed: () => deleteData(data.documentID),
),

subtitle: new Container(
padding: const EdgeInsets.only(top: 5.0),
child: new Text(
record.address,
style: new TextStyle(color: Colors.red, fontSize: 12.0,),
),
),
),
),
);
}
}

最佳答案

  • 删除单个文档:
    var collection = FirebaseFirestore.instance.collection('users');
    await collection.doc('document_id').delete();
  • 删除部分文件:
    var collection = FirebaseFirestore.instance.collection('users');
    var snapshot = await collection.where('age', isGreaterThan: 20).get();
    for (var doc in snapshot.docs) {
    await doc.reference.delete();
    }
  • 删除所有文件:
    var collection = FirebaseFirestore.instance.collection('users');
    var snapshot = await collection.get();
    for (var doc in snapshot.docs) {
    await doc.reference.delete();
    }
  • 关于firebase - Flutter/Firebase-文档 ID 未知时如何执行删除文档功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62280835/

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