数据未使用StreamBuilder从Firebase删除

时间:2019-05-11 14:54:30

标签: android flutter stream google-cloud-firestore

我正在尝试从Firebase删除一些数据并更新我的列表视图,但是我似乎无法从Firestore中删除所选数据。

我尝试使用firestore实例删除方法,但似乎没有任何反应。

 class BookList extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
 return StreamBuilder<QuerySnapshot>(
  stream: Firestore.instance.collection('Inventory').snapshots(),
  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (snapshot.hasError)
      return new Text('Error: ${snapshot.error}');
    switch (snapshot.connectionState) {
      case ConnectionState.waiting: return new Text('Loading...');
      default:
        return new ListView(
          children: snapshot.data.documents.map((DocumentSnapshot 
          document) {
            return EachList(document["Quantity"], document["Name"], 
      document);
          }).toList(),
        );
    }
    },
   );
  }
}

class EachList extends StatefulWidget {
 final DocumentSnapshot documentSnapshot;
 final String details;
 final String name;

 EachList(this.name, this.details, this.documentSnapshot);

 @override
_EachListState createState() => _EachListState(this.name, this.details, 
this.documentSnapshot);
}

class _EachListState extends State<EachList> {
 final String details;
 final String name;
 final DocumentSnapshot documentSnapshot;

_EachListState(this.details, this.name, this.documentSnapshot);
 @override
 Widget build(BuildContext context) {
  return Slidable(
  delegate: new SlidableDrawerDelegate(),
  actionExtentRatio: 0.25,
  actions: <Widget>[
    new IconSlideAction(
        caption: 'Edit',
        color: Colors.blueAccent,
        icon: Icons.edit_attributes,
        onTap: () =>
        {
        /*createAlertDialog(context, name, details)*/
        }
    ),
    new IconSlideAction(
        caption: 'Delete',
        color: Colors.red,
        icon: Icons.delete,
        onTap: () async {
          confirmDialog1(context,documentSnapshot).then((bool value) {
            print("Value is $value");
          });
        }
    ),
  ],
  child: new Container(
    padding: EdgeInsets.all(8.0),
    child: new Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        new Row(
          children: <Widget>[

            new CircleAvatar(child: new Text(name[0].toUpperCase()),),
            new Padding(padding: EdgeInsets.all(10.0)),
            new Text(name, style: TextStyle(fontSize: 20.0),),
          ],
        ),
        new Text(details, style: TextStyle(fontSize: 20.0))
      ],
    ),
  ),
  );
 }
}

Future<bool> confirmDialog1(BuildContext context, DocumentSnapshot 
 document) {
 return showDialog<bool>(
  context: context,
  barrierDismissible: false, // user must tap button!
  builder: (BuildContext context) {
    return new AlertDialog(
      title: new Text('Delete'),
      content: new Text('Are you sure you want to delete?'),
      actions: <Widget>[
        new FlatButton(
          child: const Text('NO'),
          onPressed: () {
            Navigator.of(context).pop(true);
          },
        ),
        new FlatButton(
          child: const Text('YES'),
          onPressed: () {
            Firestore.instance
                .collection('Inventory')
                .document(document.toString())
                .delete()
                .catchError((e) {
              print(e);
            });
            Navigator.of(context).pop(true);
          },
        ),
      ],
    );
  });
}

我希望它从列表和Firestore中删除数据,但是当我在确认对话框按钮上按“是”时,它似乎没有任何作用。任何帮助将不胜感激

0 个答案:

没有答案