简体   繁体   English

将获取的值传递给对 flutter 的 streambuilder 的 firestore 引用

[英]Pass fetched value to a firestore reference to flutter's streambuilder

I'm accessing a user's favorite group which is inside groupfav in Firestore, when I get it I want to give it as part of the reference to the streambuilder stream: , so that it knows what to show in a list, but I can't pass the variable that contains the favorite group, what should I do or what am I doing wrong?我正在访问 Firestore 中groupfav内用户最喜欢的组,当我得到它时,我想将它作为对 streambuilder stream:的参考的一部分,以便它知道在列表中显示什么,但我不能' t传递包含最喜欢的组的变量,我应该做什么或者我做错了什么?

static String? userID = FirebaseAuth.instance.currentUser?.uid; // get current user id
  static var taskColeccion = FirebaseFirestore.instance.collection("usuarios");
  var tack = taskColeccion.doc("$userID").get().then((value) {
    var groupfav = value.data()!["groupfav"]; // value i get from firestore
    return groupfav;
  });

  late Stream<QuerySnapshot> task = FirebaseFirestore.instance
  .collection("groups")
  .doc(groupfav) // pass the obtained value 
  .collection("tareas")
  .snapshots();

photo of firestore Firestore 的照片

The photo shows how Firestore's logic is and the value marked in green is what I must pass to the late Stream<QuerySnapshot> task... in its reference, logically it is a random value that I would not know.照片显示了 Firestore 的逻辑,绿色标记的值是我必须传递给late Stream<QuerySnapshot> task...在它的参考中,逻辑上它是一个我不知道的随机值。 thanks for any help!谢谢你的帮助!

this is what the code looks like now (I took things that were not important)这就是代码现在的样子(我拿走了不重要的东西)

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

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

class _HomeState extends State<Home> {
  static String? userID = FirebaseAuth.instance.currentUser?.uid;
  static final taskColeccion =
      FirebaseFirestore.instance.collection("usuarios");
  String groupfav = '';
  final tack = taskColeccion.doc("$userID").get().then((value) {
    groupfav = value.data()!["groupfav"];
    return groupfav;
  });

  Stream<QuerySnapshot> task = FirebaseFirestore.instance
      .collection("groups")
      .doc(groupfav) // pass the obtained value
      .collection("tareas")
      .snapshots();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Home"),
        automaticallyImplyLeading: false,
      ),
      body: StreamBuilder(
        stream: task,
        builder: (
          BuildContext context,
          AsyncSnapshot<QuerySnapshot> snapshot,
        ) {
          if (snapshot.hasError) {
            return const Text("error");
          }
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Text("cargando");
          }
          final data = snapshot.requireData;

          return ListView.builder(
            itemCount: data.size,
            itemBuilder: (context, index) {
              return Card(
                child: ListTile(
                  title: Text("${data.docs[index]['titulo']}"),
                  subtitle: Text("${data.docs[index]['contenido']}"),
                  onTap: () {},
                  trailing: IconButton(
                    icon: const Icon(Icons.delete),
                    color: Colors.red[200],
                    onPressed: () {
                      // delete function
                    },
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }
}

You just need to declare groupfav outside of the scope of the get method of taskColeccion ;您只需要在 taskColeccion 的get方法的groupfav之外声明taskColeccion

The way you have it, the variable no longer exists by the time you're trying to pass it into the task stream.按照您的方式,当您尝试将变量传递给task stream 时,该变量已不存在。

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

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

class _HomeState extends State<Home> {
  static String? userID = FirebaseAuth.instance.currentUser?.uid;
  static final taskColeccion =
      FirebaseFirestore.instance.collection("usuarios");

  String groupfav = '';

  late Stream<QuerySnapshot> task;

  @override
  void initState() {
    super.initState();
    taskColeccion.doc("$userID").get().then((value) {
      groupfav = value.data()!["groupfav"];
      return groupfav;
    });

    task = FirebaseFirestore.instance
        .collection("groups")
        .doc(groupfav) // pass the obtained value
        .collection("tareas")
        .snapshots();
  }

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

相关问题 StreamBuilder 未更新 Flutter 中的 Firestore 数据 - StreamBuilder not updating Firestore data in Flutter Flutter Firebase 查询 Firestore 和 Streambuilder() 的问题 - Flutter Firebase Problems with Query Firestore and Streambuilder() Flutter 云Firestore StreamBuilder<documentsnapshot> 错误</documentsnapshot> - Flutter cloud firestore StreamBuilder<DocumentSnapshot> error 使用 Flutter 和 Firestore 中的新数据更新 StreamBuilder - Updating a StreamBuilder with new data in Flutter and Firestore .where() 不适用于 StreamBuilder steam | firestore 查询需要一个索引 - Flutter - .where() not working with StreamBuilder steam | firestore query require an index - Flutter 在 flutter 中使用 withConverter 获取 Firestore 文档引用字段的数据 - Get Firestore documents reference field's data using withConverter in flutter Flutter:更新单个文档时,StreamBuilder 获取其他 Firestore 文档 - Flutter: StreamBuilder gets other firestore documents when updating a single document 从 flutter 中从 firestore 获取的列表中删除元素 - Deleting an element from a list fetched form firestore in flutter 使用 Firestore 为 StreamBuilder 创建 Stream - Creating Stream for StreamBuilder with Firestore 如何使用futurebuilder/streambuilder(flutter)从firestore获取数组/地图并使用lisview显示 - How to get array/map from firestore and display with lisview using futurebuilder/streambuilder(flutter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM