ListTile Flutter - firebase

is there any way I can add more value to the ListTile in the code as I am trying to use ListTile to call the value of data in FireCloud. As for now, I can only call 2 of the data that is in the document. Any help would be much appreciated on how to call multiple data in FireCloud and display it.
return Container(
child: Card(
child: ListTile(
title: Text(widget.post.data["Address"]),
subtitle: Image.network(widget.post.data["Picture"],
),
),
),
);

Check out this example that will work for you :
ListView.builder(
itemCount: /* snapshot.data.length */,
itemBuilder: (_, index) {
return GestureDetector(
onTap: (){
//navigateToDetail(snapshot.data[index]),
},
child: Card(
child: Column(
children: <Widget>[
Text('your address'),
Text('you and another data')
],
),
),
);
}),
You ca align it in the way you want.
Let me know if it works

Use ListView.builder constructor, this will help you fetch the total data from the FireCloud. You can use it like this:
Two major things required, these are:
itemCount: Keeps the count of the data you are going to fetch from the FireCloud. Store the coming data into a list, and then pass it into this key
itemBuilder: Builds your view which will have the data used from the passed item in the itemCount
Reference:
ListView.builder(
itemCount: listItems.length,
itemBuilder: (BuildContext ctxt, int index){
// your_view
}
)
For more reference, please refer to this Medium Article on how to get started: Flutter: Displaying Content using ListView.builder

Related

How to show data in Flutter from Firestore collection based on the value in the array?

I need to remove documents - reviews/posts - from the stream, which the user has hidden. When a user decides to hide the post, the post collection's post-doc creates an array 'hidingUserId' of userIDs that has hidden it. refer to the pic, pls.
now I need to remove the post where the array 'hidingUserId' contains the currentUserId. I have tried the following, but it does not display errors in the log and it does not display the posts. Just circular progress circling on the screen.
Stream streams = postRef
.orderBy('stars', descending: false)
.orderBy('timestamp', descending: true)
.snapshots();
#override
Widget build(BuildContext context) {
return ListView(
shrinkWrap: true,
padding: EdgeInsets.symmetric(horizontal: 10.0),
children: [
StreamBuilderWrapper(
shrinkWrap: true,
stream: streams.where((event) => event.data()['hidingUserId']
== null
|| event.data()['hidingUserId'].contains(!currentUserId()) ?
streams.forEach((event) => print(event))
: Container(
child: Center(
child: Text('Nothing to show'),))),
physics: NeverScrollableScrollPhysics(),
itemBuilder: (_, DocumentSnapshot snapshot) {
internetChecker();
Review posts = Review.fromJson(snapshot.data());
return posts.postId != null
? Padding(
padding: const EdgeInsets.only(bottom: 12.0),
child: Posts(post: posts),
)
: Container(
child: Center(
child: Text('Nothing to show'),
),
);
},
),
],
);
}
am new to Firestore, and I can't find the information I need. help appreciated very much! Thanks!

How do I fetch sub collection from firestore using flutter?

I wanna fetch the data inside the Guests like guestName. The code below I used it to fetch collection and it worked, but I have no idea how to fetch subcollection. I been trying but it's not working.
body: CustomScrollView(
slivers: [
SliverPersistentHeader(pinned: true, delegate: SearchBoxDelegate()),
StreamBuilder<QuerySnapshot>(
//I used this to fetch collection but it doesn't work with subcollection
stream: Firestore.instance
.collection("Guests")
.orderBy("Date", descending: true).snapshots(),
builder: (context, dataSnapshot) {
return !dataSnapshot.hasData
? SliverToBoxAdapter(
child: Center(
child: circularProgress(),
),
)
: SliverStaggeredGrid.countBuilder(
crossAxisCount: 1,
staggeredTileBuilder: (c) => StaggeredTile.fit(1),
itemBuilder: (context, index) {
ItemModel model = ItemModel.fromJson(
dataSnapshot.data.documents[index].data);
return sourceInfo(model, context);
},
itemCount: dataSnapshot.data.documents.length,
);
},
),
],
),
Container(
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Text(
// this guestName i wanna fetch
model.guestName,
style: TextStyle(
color: Colors.black, fontSize: 14.0),
),
),
],
),
),
To get a CollectionReference for a subcollection, you first need to have a DocumentReference object to its parent document, and then you can call collection() on that.
In your builder you have dataSnapshot.data.documents[index], which gives you a DocumentSnapshot object, from which you can get a DocumentReference by calling reference on it.
So combined that'd be something like this in your builder:
dataSnapshot.data.documents[index].reference.collection("Guests")
I recommend always keeping the reference documentation handy that I linked above, as it's the easiest way to find this type of API path.

Flutter / Firestore - How can i fetch collection in subcollection userid equal to currentuser

My Firestore structure here,
i want to fetch lesson data.But only users with equal user ids in the member collection.But i can't.
is that worst usage?
body: StreamBuilder(
stream: firestore
.collection('lessons')
.where('members', isEqualTo: FirebaseAuth.instance.currentUser.uid)
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
} else {
return Container(
child: ListView(
scrollDirection: Axis.vertical,
children: snapshot.data.docs.map((document) {
return Card(
margin: EdgeInsets.all(5),
child: Column(
children: [
ListTile(
title: Text(document['lessonTitle']),
leading: Icon(Icons.label),
trailing: Text(document['lessonSubtitle']),
)
],
),
);
}).toList(),
),
);
}
},
),
There is no way to select documents from one collection based on values in another (sub)collection. Queries in Firestore can only consider information in the documents that they're returning.
If you want to select lessons based on their members, you'll need to include that information in each lesson document itself. The common way to do this is to add a members array to each lesson document, use arrayUnion and arrayRemove operations to manipulate that array, and then use array-contains to query the array.

How to display any Data from Firestore using ternary operator

I was trying to fetch data from firebase and I wanted to show an Icon of Instagram only if there is any data available in the firebase collection. I used the following method to fetch the data from firebase
StreamBuilder(
stream: Firestore.instance.collection("aboutpage").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container(
child: Center(
child: SpinKitThreeBounce(
color: Colors.lightBlueAccent,
size: 30.0,
),
),
);
} else {
return Container(
child: ListView.builder(
physics: ClampingScrollPhysics(),
shrinkWrap: true,
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot aboutpage = snapshot.data.documents[index];
return Column(
There is no Problem while retrieving data from firebase. Probleme is when I want to display an Instagram icon if the data is available in firebase is used ternary operator but did not work for me
"${aboutpage["name"]}" == null
? Container()
: IconButton(Icon: Icons.instagram),
onPressed: (}{}
),
I also tried using "${aboutpage["name"]}".isEmpty but dint work for me. So, How can I fetch data only if its available and if empty return empty Container?
Change the condition from "${aboutpage["name"]}" == null to aboutpage is Map && aboutpage["name"] == null.

Retrieve Map Data in Firebase to Flutter

I have this data structure
Database Structure
I need to call all the item in the FavDestination collection and here is my code in Flutter
child: StreamBuilder(
stream: Firestore.instance.collection('FavDestination').snapshots(),
builder: (context, snapshot){
if(!snapshot.hasData) return Text('Loading Data... Please Wait');
return
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.documents.length, //snapshot.data.document.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot destination = snapshot.data.documents[index];
//DocumentSnapshot destination = snapshot.data.document[index];
return GestureDetector(
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => DestinationScreen(
destination: destination,
),
),
),
It retrieves the data successfully, and if the user taps on the document, it will go to Destination Screen and I need to retrieve all the data in activities, here is my code in the Destination Screen
Expanded(
child: ListView.builder(
padding: EdgeInsets.only(top: 10.0, bottom: 15.0),
itemCount: widget.destination['activities'].length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot activity = widget.destination['activities'][index];
return Stack(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 120.0,
child: Text(
activity['name'],
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.w600,
),
and here is the Error I got
Error Message
any idea how to solve it? Thank you
It seems that your activities field is nested map. On first level you have field Bali1 which is map as well. And it the code activity is trying to get name on first level.
I suppose you should work more on destination object structure. You can print it out to analyze and try to map object like Map<dynamic, dynamic> and Bali1 as well.
I don't have playground to do it fast, but I found similar approach here:
How do I get specific values from a datasnapshot in flutter?
I hope it will help!

Resources