Flutter Streambuilder is duplicating items - flutter

I have a StreamBuilder connected to a List and the List gets its data from Firebase, but every there is an Event in my Database the items in my Streambuilder get Duplicated.
Here is my StreamBuilder code:
StreamBuilder(
stream: masterListStart().asStream(),
builder: (context, snapshot) {
//return coursesList.length == 0
return finishedLoadingList
? ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: storysList.length,
itemBuilder: (context, index) {
//
StoryItems data = storysList[index];
//
return StoryItems(
data: data,
);
},
)
: CircularProgressIndicator();
},
),
How I can prevent the StreamBuilder from doing this?

Instead of using finishedLoadingList, you can simply check your snapshot connectionState as following
StreamBuilder(
stream: masterListStart().asStream(),
builder: (context, snapshot) {
//return coursesList.length == 0
return (ConnectionState.done == snapshot.connectionState) ? ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: storysList.length,
itemBuilder: (context, index) {
//
StoryItems data = storysList[index];
//
return StoryItems(
data: data,
);
},
) : CircularProgressIndicator();
},
),

Related

How to listen to stream and update Flutter GridView builder accessing Firebase

I need an example code in which a Flutter Gridview builder streams Firebase data (for example with a single Collection and multiple Documents with multiple fields in each) that can be modified by the user straight from the Gridview
I've watched this tutorial => https://www.youtube.com/watch?v=ErP_xomHKTw
Although it shows how to access and modify data from Firebase I still can't wrap my head around the Firebase and Gridview builder interaction.
Stream Builder using GridView
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('products').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return GridView.builder(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200,
childAspectRatio: 3 / 2,
crossAxisSpacing: 20,
mainAxisSpacing: 20),
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot doc = snapshot.data!.docs[index];
return Column(
children:[
Text(doc['name']),
Text(doc['price']);
);
);
}),
} else {
return Text("No data");
}
},
)
Stream Builder using ListView
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('products').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot doc = snapshot.data!.docs[index];
return Column(
children:[
Text(doc['name']),
Text(doc['price']);
);
});
} else {
return Text("No data");
}
},
)

How to show list more than 0 up of string in array flutter

I have a array here and I want to show all of it accepted for the first one as discibe in an image:
And this is the code:
StreamBuilder(
stream: FirebaseFirestore.instance
.collection("groups")
.doc(groupId)
.snapshots(),
builder:(context, AsyncSnapshot<DocumentSnapshot> snapshot) {
var userDocument = snapshot.data?["members"];
return ListView.builder(
scrollDirection: Axis.horizontal,
physics:const BouncingScrollPhysics(),
itemCount: userDocument.length,
shrinkWrap:true,
itemBuilder:(context, index) {
//Where I display all my list of members
return Text(userDocument[index]);
}),
This is what it's look like in simulator
But it still displays all of it And I dont want to display the first one
Try the following code:
ListView.builder(
itemCount: userDocument.length,
itemBuilder: (BuildContext context, int index) {
return index == 0 ? Container() : write your code here;
},
),
Just tell it that the item count is one less, and then access the document one higher index. Like
return ListView.builder(
scrollDirection: Axis.horizontal,
physics:const BouncingScrollPhysics(),
itemCount: userDocument.length - 1,
shrinkWrap:true,
itemBuilder:(context, index) {
//Where I display all my list of members
return Text(userDocument[index + 1]);
}),
here you can skip the index 0 and display rest
ListView.builder(
itemCount: userDocument.length,
itemBuilder: (BuildContext context, int index) {
if(index == 0) {
// return container();
} else {
return Text(userDocument[index]);
}
},
),
You can skip the first index by
StreamBuilder(
stream: FirebaseFirestore.instance
.collection("groups")
.doc(groupId)
.snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
var userDocument = snapshot.data?["members"];
if(userDocument.isNotEmpty){
userDocument.remove(userDocument.first);
}
return ListView.builder(
scrollDirection: Axis.horizontal,
physics: const BouncingScrollPhysics(),
itemCount: userDocument.length,
shrinkWrap: true,
itemBuilder: (context, index) {
//Where I display all my list of members
return Text(userDocument[index]);
});
});

my list of array snapshot is in vertical flutter

I try to get the snapshot of an array and I get it But when I display it in my app It’s in vertical as describe in the
image:
And This is the normal one in my cloud firestore (I want to display it in an horizontal ofcourse)
And here's the
code:
StreamBuilder(
stream: FirebaseFirestore.instance
.collection("groups")
.doc(groupId)
.snapshots(),
builder: (context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
var userDocument = snapshot.data?["members"];
if (!snapshot.hasData) {
return Container();
}
return ListView.builder(
//physics: const BouncingScrollPhysics(),
itemCount: userDocument.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection("users")
.doc(userDocument[index])
.snapshots(),
builder: (context, snapshot1) {
var userDocument = snapshot1.data?['fullName'];
if (snapshot1.data == null) {
return const Text('No Data');
}
return ListView.builder(
itemCount: userDocument.length,
shrinkWrap: true,
itemBuilder: (context, index) {
//Where I display the name in app
return Center(
child:
Text(userDocument[index]));
});
});
});
})
Please take a look at the code
Give your ListView.builder a height by wrapping it inside a SizedBox.
Then inside your ListView.builder, there is a parameter scrollDirection: Axis.horizontal,. Next, you can disable scrolling too.

Flutter combine futuerbuilder and streambuilder

i need some guidance how to combine a futurebuilder with a streambuilder. For example, the futurebuilder will load the comments which are stored in my Mysql database, while the streambuilder will fetch new comments and add them to the listview. The stream and future works perfectly, but i have no idea how i will combine these two things with eachother.
FutureBuilder(
future: _fetchComments(39),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
return Container(
height: 150,
child: StreamBuilder(
stream: commentProvider.channel.stream,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot2) {
return ListView.builder(
key: PageStorageKey("CommentsScroll"),
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
final comment = snapshot.data[index];
return CommentTile(
key: Key(DateTime.now().toString()),
photoUrl: comment["photo_url"],
displayName: comment["display_name"],
created: comment["created"],
text: comment["text"],
);
},
);
},
),
);
},
),

Fetching particular data from firestore using Flutter

I am using Firestore as a database with my Flutter Application. I am storing data in the following way:
I want to get data of shop based upon document ID(which is the id of currently logged in user). I am using following code but unable to fetch particular data.
And using stream builder for to display data.
StreamBuilder(
stream: Firestore.instance
.collection('Shops')
.document(user.uid)
.snapshots(),
builder: (context, snapshot) {
print(snapshot.hasData);
if (snapshot.hasData) {
return ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, i) {
return SingleChildScrollView(
child: Column(children: <Widget>[
_buildRow(context, snapshot, i),
SizedBox(
height: 20,
),
_buildShopDetails(context, snapshot, i),
_buildReviewsRatingCard(snapshot, i),
]),
);
});
} else {
return CircularProgressIndicator();
}
},
),
So kindly help me in solving my problem.