Flutter StreamBuilder Called Data 3times - flutter

return Container(
color: Colors.white,
child: StreamBuilder<QuerySnapshot>(
stream: _statusService.getStatus(),
builder: (context, AsyncSnapshot snapshot) {
return !snapshot.hasData
? CircularProgressIndicator()
: ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot movies = snapshot.data.docs[2];
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 0,
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 0,
),
child: Text(
"${movies['description']}",
style: TextStyle(
fontSize: 10,
color: Color(0xFF737599),
),
),
),
);
});
}),
);
}
}
can someone help me i am trying to call some data from firebase but its called 3 times i didn't find any solution i am waiting for your answers

I think your data length was 3 but you only show doc2. For the length of doc 3, data is displayed 3 times.
change
itemCount: snapshot.data.docs.length,
To
itemCount: 1,

Related

How to group photos by time in Flutter?

I want the photos taken on the same date to be in the same group.
This is my gallery
I want this
Future<List<VideoImageDataModel>> fetchPhotos() async {
var dataList = await Historydao().ImageData();
return dataList;
}
FutureBuilder<List<VideoImageDataModel>>(
future: fetchPhotos(),
builder: (context, snapshot) {
if (snapshot.hasData) {
var photoList = snapshot.data;
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: (10 / 10), crossAxisCount: 4),
itemCount: photoList?.length,
itemBuilder: (BuildContext context, int index) {
return Container(
margin: EdgeInsets.fromLTRB(5, 5, 0, 0),
//padding: EdgeInsets.fromLTRB(10, 5, 0, 0),
child: Hero(
tag: photoList![index].thumbnail,
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImagePage(
photoList[index]
.thumbnail
.toString())));
},
child: Stack(fit: StackFit.expand,
children: [
Image.file(
fit: BoxFit.cover,
File(photoList[index].thumbnail.toString()),
),
Positioned(
right: 3,
bottom: 3,
child: Column(crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'${DateFormat('Hm', 'en_US').format(DateTime.fromMillisecondsSinceEpoch(photoList[index].id))}',
style: const TextStyle(
color: Colors.white,
fontSize: 9,
fontWeight: FontWeight.bold),
),
Text(
'${DateFormat('dd/MM/yyyy').format(DateTime.fromMillisecondsSinceEpoch(photoList[index].millisecond))}',
style: TextStyle(
color: Colors.white,
fontSize: 9,
fontWeight: FontWeight.bold),
), ],),),],),),),);}, );
} else {
return const Center(
child: CircularProgressIndicator(
color: Colors.blue,
),
);
}
},
),
I am creating my gallery using gridview builder.Thumbnail is photo name.I am generating date data using milliseconds.I want to show photos taken on the same day as a group.I can find the changed date using for loop.i don't know how to do this.

Flutter StreamBuilder with duplicated snapshot

I have a StreamBuilder in order to build a Chat App, but there's an issue that it unexpectedly reloads with some events. At the time it reloads, the snapshot still got the same data in it, therefore some messages are repeatedly added to the List 'messages'.
How can I prevent the StreamBuilder from doing this repetition?
Here is my StreamBuilder code:
body: SingleChildScrollView(
reverse: true,
child: Center(
child: StreamBuilder(
stream: channel.stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
addText(snapshot.data.toString(), 1); // adds to messages
}
return ListView.builder(
shrinkWrap: true,
padding: EdgeInsets.only(top: 10, bottom: 10),
physics: NeverScrollableScrollPhysics(),
itemCount: messages.length,
itemBuilder: (context, index) {
return Container(
padding: EdgeInsets.only(
left: 14, right: 14, top: 10, bottom: 10),
child: Align(
alignment: (messages[index].messageType == 'receiver'
? Alignment.topLeft
: Alignment.topRight),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: (messages[index].messageType == 'receiver'
? Colors.grey.shade200
: Colors.blue[200]),
),
padding: EdgeInsets.all(16),
child: Text(
messages[index].messageContent,
style: TextStyle(fontSize: 15),
),
),
),
);
},
);
},
),
),
),
create a List in your state class andif(snapshot.hasData){ List = snapshot.data; }
then use the items in your list instead of your snapshot;

Firebase call data

This is my first code:
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 0,
),
child: Text(
" i want get data here",
style: TextStyle(
color: Color(0xFF737599),
),
),
),
thats my second code :
return Container(
color: Colors.white,
child: Container(
height: size.height * 0.4,
child: StreamBuilder<QuerySnapshot>(
stream: _statusService.getStatus(),
builder: (context, AsyncSnapshot snapshot) {
return !snapshot.hasData
? CircularProgressIndicator()
: ListView.builder(
itemCount: 1,
itemBuilder: (context, index) {
DocumentSnapshot movies = snapshot.data.docs[2];
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 0,
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 0,
),
child: Text(
"${movies['description']}",
style: TextStyle(
fontSize: 10,
color: Color(0xFF737599),
),
),
),
);
});
}),
),
);
I want to pull a text from firebase. I managed to do this (on a different dart page), but how can I integrate it into the code I wrote before (there was a part where I printed the text manually without using firebase, how can I integrate it into that part).
In short, I want to integrate the text part in the first code, the part where I get data from the pharaoh in the second code.
I'm sharing both parts of the code, can you help?
Ok this is not a best practice what you do but you can create class attribute and you can assgin'it in your second code like
var yourVar = "";
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 0,
),
child: (yourVar!="")?Text(
yourVar,
style: TextStyle(
color: Color(0xFF737599),
),
) ?null,
),
```
in your second code
```
return Container(
color: Colors.white,
child: Container(
height: size.height * 0.4,
child: StreamBuilder<QuerySnapshot>(
stream: _statusService.getStatus(),
builder: (context, AsyncSnapshot snapshot) {
return !snapshot.hasData
? CircularProgressIndicator()
: ListView.builder(
itemCount: 1,
itemBuilder: (context, index) {
DocumentSnapshot movies = snapshot.data.docs[2];
setState(() { yourVar= newValue; });
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 0,
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 0,
),
child: Text(
"${movies['description']}",
style: TextStyle(
fontSize: 10,
color: Color(0xFF737599),
),
),
),
);
});
}),
),
);

Make the ListView.builder() show a fraction of the next (or previous) element

How can Make the ListView.builder() show a fraction of the next (or previous) element.
I have a ListView.builder() and it works fine but I want the user to know that there is a scrolling functionality and that they need to scroll.
How can achieve that?
My output:-
Expected output:-
How can achieve that?
Here is my code
return FutureBuilder(
future: categories,
builder: (BuildContext context, AsyncSnapshot snapshot) {
return ListView.builder(
itemCount: snapshot.data.length,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return GestureDetector(
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Container(
child: Padding(
padding:
const EdgeInsets.fromLTRB(16.0, 7.0, 16.0, 10.0),
child: Text(
snapshot.data[index].name,
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
color: colors[index],
),
),
),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Posts(
type: PostsFetchByType.categories,
categoryOrTagIds: [snapshot.data[index].id],
))),
);
});
});
Create a scrollcontroller with an initial offfset:
scrollController = ScrollController(
initialScrollOffset: 10 // put your value here
);
then add it to your ListViewBuilder:
return ListView.builder(
...
controller: scrollController,
...
)
But since you want to show that it is scrollable, you can also have a look at adding scrolling bar by wrapping ListViewBuilder as follows:
Scrollbar(
child: ListView.builder(...),
)

streambuilder not displaying data in list

I'm trying to display a list of employees in my app. Here's the code I got so far:
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 20,top: 20),
child: Container(
height: 40,
width: 120,
decoration: BoxDecoration(
color: Colors.grey.withOpacity(0.12),
borderRadius: BorderRadius.circular(8)),
child: Padding(
padding: const EdgeInsets.all(10),
child: Text("Employees",
style: TextStyle(
fontSize: 22,
color: Colors.black,
fontWeight: FontWeight.w600,
)),
),
),
),
StreamBuilder(
stream: Firestore.instance.collection("Employees").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(child: Text("Fetching data..."));
} else if(snapshot.connectionState == ConnectionState.done){
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(snapshot.data.documents[index]['name']));
},
);
} else {
return Text("hol up");
}
}),
],
);
When I run the app, it just shows 'Employees' and 'hol up' in a column. Why does the list of employees not show?
I solved the issue by simply adding shrinkWrap: true in my listview.
This should work. Also get the data from the indexed document.
ListTile(
title: Text(snapshot.data.documents[index].data['name']));