简体   繁体   English

如何在 Listview 中编辑特定的压制卡并将其更新为 firestore flutter

[英]How to Edit specific pressed card in Listview and update it to firestore flutter

I have a streambuilder that streams data from education collection and display a listview consist of cards.我有一个 streambuilder,它从教育集合中流式传输数据并显示由卡片组成的列表视图。 I have added gesture detector with edit icon in trailing of listview.我在列表视图的尾部添加了带有编辑图标的手势检测器。

This is my streambuilder:这是我的 streambuilder:

StreamBuilder<
    List<MyEducation>>(
stream: readEducation(),
builder:
    ((context, snapshot) {
  if (snapshot.hasError) {
    return Text(
        'Something went wrong ${snapshot.error}');
  } else if (snapshot
      .hasData) {
    final educations =
        snapshot.data!;

    return ListView(
      scrollDirection:
          Axis.vertical,
      shrinkWrap: true,
      children: educations
          .map(
              buildEducation)
          .toList(),
    );
  } else {
    return Center(
      child:
          CircularProgressIndicator(),
    );
  }
},),
)//streambuilder

This is the stream of list and the ListTile这是列表流和 ListTile

 Stream<List<MyEducation>> readEducation() => FirebaseFirestore.instance
      .collection('education')
      .snapshots()
      .map((snapshot) => snapshot.docs
          .map((doc) => MyEducation.fromJson(doc.data()))
          .toList());

  Widget buildEducation(MyEducation educationss) => ListTile(
        enabled: isEnabled,
        leading: CircleAvatar(
          child: Image.network(educationss.universityImage),
        ),
        title: Text(educationss.universityName),
        // The icon button which will notify list item to change
        trailing: GestureDetector(
          child: new Icon(
            Icons.edit,
            color: Colors.black,
          ),
          onTap: () {
            openDialog(); //the opendialog for form
            setState(() {
              isEnabled = !isEnabled;
            });
          }, //this is the button edit
        ),
      );

After the opendialog is opened, I display form any user insert the data again then run this.打开 opendialog 后,我会显示任何用户再次插入数据然后运行它。

This is just the CreateEducation method:这只是CreateEducation方法:

Future createEducationCollege() async {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => const Center(
        child: CircularProgressIndicator(),
      ),
    );

    try {
      String date1 = selectedMonthStart! + " " + selectedYearStart.toString();
      print(date1);
      // DateFormat datestart = DateFormat.yMMMM(date1);
      // print(datestart);
      String date2 = selectedMonthEnd! + " " + selectedYearEnd.toString();
      print(date2);
      // DateFormat dateend = DateFormat.yMMMM(date2);
      // print(dateend);
      print(FirebaseAuth.instance.currentUser?.uid);
      final docEducation =
          FirebaseFirestore.instance.collection("education").doc();
      final uid = FirebaseAuth.instance.currentUser!.uid;
      final userEducation = MyEducation(
        uid: uid,
        universityName: collegeAheadController.text,
        universityImage: imageuniversitycollege,
        major: SelectedMajor.toString(),
        degree: SelectedDegree.toString(),
        dateEnd: date1,
        dateStart: date2,
      );
      final json = userEducation.toJson();

      await docEducation.set(json);
    } on FirebaseException catch (e) {
      Utils.showSnackBar(e.message);
    }

    //Navigator.of(context) not workking!!
    navigatorKey.currentState!.popUntil((route) => route.isFirst); 
}

How to change this into update the selected item for the firestore database?如何将其更改为更新 firestore 数据库的选定项目?

这是显示器

Actually I have no experience in edit selected data.实际上我没有编辑所选数据的经验。 I have search about edit selected data but its usually about users collection.我搜索了有关编辑所选数据的信息,但通常是关于用户集合的。 How can I make the system know which list to change?我怎样才能让系统知道要更改哪个列表? I'm thinking of compare using equal to in.where() and change.set to.update.我正在考虑使用等于 in.where() 和 change.set to.update 进行比较。

Streambuilder listens to events coming from the stream not from your variables.You can have your stream as a state variable and when you change your date change your stream and the data will be refreshed.Widget rebuilding is scheduled by each interaction, using State.setState, but is otherwise decoupled from the timing of the stream.The FlutterFire documentation includes an example on how to listen to Firestore for events that can be used to update the widget. Streambuilder 侦听来自流而不是来自您的变量的事件。您可以将流作为状态变量,当您更改日期时更改您的流,数据将被刷新。Widget 重建由每次交互安排,使用 State.setState ,但在其他方面与流的时间分离。FlutterFire 文档包含一个示例,说明如何侦听 Firestore 以获取可用于更新小部件的事件。 The same documentation page has a guide on how to update a value in the Firestore document with the update method.同一个文档页面有一个关于如何使用 update 方法更新 Firestore 文档中的值的指南

DatabaseReference databaseRef; 
// initState() databaseRef = _database.child(widget.latestPath); 
// build return StreamBuilder( stream: databaseRef.onValue, builder: (context, snapshot) { // Do something with the data }, );
// When u want to update new path setState(() { databaseRef = _database.child(widget.newPath);; 
});

While working with the pressed card and widgets you need to wrap your functions with setState.在使用按下的卡片和小部件时,您需要使用 setState 包装您的函数。 onPressed: () { setState(() { _visibleBehavior = true; }) } Also you can try and use the setState method and refactor your pressed card details from StatelessWidget to StatefullWidget. onPressed: () { setState(() { _visibleBehavior = true; }) }您也可以尝试使用 setState 方法并将您按下的卡片详细信息从 StatelessWidget 重构为 StatefullWidget。
You can check the following similar examples:您可以检查以下类似示例:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更新 flutter 中 firebase 火库中的所有集合 - How to update all collection in firebase firestore in flutter Flutter - Firestore - 列出来自集合字段的字符串,并在按下“保存设置”后更新 Fire Store - Flutter - Firestore - List String to be from collection field and update fire store once "Save Setting' is pressed 如何在 flutter 中实现刷卡并显示来自 Firestore 的交易 - How to implement the card swipe and display its transaction from firestore in flutter 如何在 Firestore 文档的 map 中添加或编辑特定值? - How to add or edit specific value within a map on a firestore document? 如何使用特定参数 flutter 从 Firestore 中删除数据? - How to delete data from Firestore with specific parameter flutter? 如何使用 flutter 和 dart 从 Cloud Firestore 检索特定数据? - how to retrieve specific data from Cloud Firestore using flutter and dart? 如何将特定字段值从 Firestore 保存到 Flutter - How to save specific field value from Firestore to Flutter 从 flutter firestore 中的子集合中获取特定文档 - fetching a specific document from a subcollection in flutter firestore 如何从 Firestore 获取特定数据并在 Objective-C 中更新它 - How to fetch specific data from Firestore and update it in Objective-C 如何在 flutter 上使用 fireart package 更新云 Firestore 上的数组值? - How can I update array value on cloud firestore using firedart package on flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM