如何在Listview中编辑特定的已按下的卡片并将其更新为firestore flutter

bvjxkvbb  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(123)

我有一个从教育收集数据流和显示由卡片组成的列表视图的流构建器。我已经添加了手势检测器与编辑图标在尾随的列表视图。
这是我的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

这是列表和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
        ),
      );

打开opendialog后,我显示窗体,任何用户都可以再次插入数据,然后运行此命令。
这就是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); 
}

如何将此更改为更新firestore数据库的选定项?

实际上,我没有编辑选定数据的经验。我搜索过编辑选定数据,但通常是关于用户集合的。我如何让系统知道要更改哪个列表?我想在.where()中使用equal to进行比较,并将.set更改为. update。

zphenhs4

zphenhs41#

Streambuilder监听来自流的事件,而不是来自变量的事件。您可以将流作为状态变量,当您更改日期时,更改流,数据将被刷新。Widget重建由每个交互使用State.setState进行调度。但是在其他方面与流的定时分离。FlutterFire文档包含一个示例,说明如何侦听Firestore以获取可用于更新小部件的事件。同一文档页上有一个指南,介绍如何使用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);; 
});

在使用按下的卡片和小部件时,您需要使用setState. onPressed: () { setState(() { _visibleBehavior = true; }) } Package 您的函数。此外,您可以尝试使用setState方法,并将按下的卡片详细信息从StatelessWidget重构为StatefullWidget。
您可以查看以下类似示例:

相关问题