将获取的值传递给 flutter 的 Streambuilder 的 Firestore 引用

发布于 2025-01-11 17:29:58 字数 2971 浏览 0 评论 0原文

我正在访问位于 Firestore 中 groupfav 内的用户最喜爱的组,当我获取它时,我想将其作为对 Streambuilder stream: 的引用的一部分,因此它知道在列表中显示什么,但我无法传递包含最喜欢的组的变量,我应该做什么或者我做错了什么?

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();

firestore的照片

该照片显示了Firestore的逻辑,绿色标记的值是我必须的传递到late Stream; task... 在其参考中,逻辑上它是一个我不知道的随机值。感谢您的帮助!

这就是代码现在的样子(我拿了一些不重要的东西)

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
                    },
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }
}

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?

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

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. 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
                    },
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日记撕了你也走了 2025-01-18 17:29:58

您只需在 taskColeccionget 方法范围之外声明 groupfav 即可;

按照您的方式,当您尝试将变量传递到 task 流时,该变量已不再存在。

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();
  }

You just need to declare groupfav outside of the scope of the get method of taskColeccion;

The way you have it, the variable no longer exists by the time you're trying to pass it into the 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();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文