0

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

1 Answer 1

1

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(); } 
Sign up to request clarification or add additional context in comments.

5 Comments

hello, thanks for the answer and explanation, but it doesn't work for me, I get the following error Error: Can't access 'this' in a field initializer to read 'groupfav'. lib/screens/home.dart:56 groupfav = value.data()!["groupfav"]; I understand that this keyword is used to denote the current instance, for the same reason it used late, but this leads me to the fact that it does not obtain the value, it remains empty
Ok, try adding the rest of the relevant auth code then because there's something going on that I can't see.
there is not much more, the part of Firebase.initializeApp() is passed in the main.dart, the rest is being imported in home.dart where all the problem is, in the same way I added all the important code above, maybe there you see more.
Oh didn't realize all that code was sitting in a stateful widget and not a function, see updated code.
Thanks, but i got the following error Exception has occurred. _AssertionError ('package:cloud_firestore/src/collection_reference.dart': Failed assertion: line 116 pos 14: 'path.isNotEmpty': a document path must be a non-empty string) I understand that it is because part of the reference is empty, but this is changed later, i tried to figure it out and changed String groupfav = ''; for String groupfav = 'groupid4'; (thinking that this would be replaced), the app worked, but the value grupoid4 was not changed, since it should be grupoid3 that's how it is in firestore

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.