I need to retrieve data from firestore collection and assign exact value to String…
Here is my code.
class _AbcState extends State<Abc> {
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('boat').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return const Center(child: Text('Something wrong!'));
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.data!.size == 0) {
return const Center(
child: Text('No boats'),
);
}
return ListView.builder(
padding: const EdgeInsets.all(15.0),
physics: const ScrollPhysics(),
shrinkWrap: true,
itemCount: snapshot.data!.size,
itemBuilder: (context, index) {
Map<String, dynamic> boatData =
snapshot.data!.docs[index].data() as Map<String, dynamic>;
return Card(
child: ListTile(
contentPadding: const EdgeInsets.all(15.0),
horizontalTitleGap: 50,
title: Text(boatData['boatName']),
subtitle: GetInfo(boatData: boatData),
),
);
},
);
},
);
}
}
class GetInfo extends StatelessWidget {
const GetInfo({
Key? key,
required this.boatData,
}) : super(key: key);
final Map<String, dynamic> boatData;
@override
Widget build(BuildContext context) {
return StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection('actor')
.doc(boatData['uid'])
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return const Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
return Text(snapshot.data!['name']);
},
);
}
}
By calling GetInfo I can get the data as a Text Widget.
But I need to get that value and assign it to a variable for future purposes.
I saved documents by user id in actor collection.
2
Answers
one you can fix this is by having a function triggered for you in the _AbcState whenever there’s a valid name in GetInfo class.
then on the listview you returning when there’s data do this…
so with this, whenever u get a valid name, the function in your _AbcState will be triggered with the corresponding name and you can do anything with the data.
Because you are calling DocumentSnapshot in the StreamBuidlder
Get the values like below