0

i have a list of strings showed in listveiw.builder widget i want to get the document info from firebase when i press on a single word

var words = [red,blue,green,yellow] ;

i want to get the document based on the value of the list

showed in :

 ListView.builder( itemCount: words.length, itemBuilder: (context, index) { return Column( children: [ Row( children: [ Expanded( flex: 3, child: InkWell( onTap: () {}, child: Text('${words[index]}'), ), ), Expanded( flex: 1, child: InkWell( onTap: () { // press this and get the data of {words[index]} document }, child: Text( 'press to get color info', ), ), ), ], ), Text( show the data here after fetching it from firestore) ], ); }, ) 

2 Answers 2

2

Let's say you have a Firestore with collection called 'words'

CollectionReference wordsFireStoreRef = FirebaseFirestore.instance.collection('words'); 

Now you have to access a document within a collection that has the word you are looking for.

ListView.builder( itemCount: words.length, itemBuilder: (context, index) { return Column( children: [ Row( children: [ Expanded( flex: 3, child: InkWell( onTap: () {}, child: Text('${words[index]}'), ), ), Expanded( flex: 1, child: InkWell( onTap: () { // press this and get the data of {words[index]} document // LOOK HERE. This line will return a type called Future<DocumentSnapshot> wordsFireStoreRef.doc(words[index]).get(); }, child: Text( 'press to get color info', ), ), ), ], ), Text( show the data here after fetching it from firestore) ], ); }, ) 

I am going to leave the rest to you. You can use FutureBuilder to build a widget or you can do this to

wordsFireStoreRef.doc(words[index]).get().then((value){ //Value is the data that was returned print(value.data()['word']); }); 
Sign up to request clarification or add additional context in comments.

Comments

0

you can read single document using a document id like this

class GetUserName extends StatelessWidget { final String documentId; GetUserName(this.documentId); @override Widget build(BuildContext context) { CollectionReference users =FirebaseFirestore.instance.collection('users'); return FutureBuilder<DocumentSnapshot>( future: users.doc(documentId).get(), builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) { if (snapshot.hasError) { return Text("Something went wrong"); } if (snapshot.hasData && !snapshot.data!.exists) { return Text("Document does not exist"); } if (snapshot.connectionState == ConnectionState.done) { Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>; return Text("Full Name: ${data['full_name']} ${data['last_name']}"); } return Text("loading"); }, ); 

} }

for more information visit https://firebase.flutter.dev/docs/firestore/usage

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.