flutter: StreamBuilder doesn't accept merged stream - flutter

i have a problem with merged stream and StreamBuilder.
im trying to merge multiple streams from firestore that each one represents a grocery list.
my result should be a ListView that combines all list in a some group.
from some reason,my StreamBuilder shows a single stream list in one tab but doesnt show it in another.
group list app photo
personal list app photo
code:
Stream<QuerySnapshot<Map<String, dynamic>>> getPesonalStream<T>() {
final userGroceries =
_fireStore.collection("users").doc(email).collection("groceries");
return userGroceries.snapshots();
}
Stream<QuerySnapshot<Map<String, dynamic>>> getGroupStream<T>() {
List<Stream<QuerySnapshot<Map<String, dynamic>>>> list = [];
_fireStore
.collection("groups")
.doc(gid)
.snapshots()
.forEach(((docSnapshot) {
List<dynamic> members = docSnapshot.data()!["members"];
list = members
.map((member) => _fireStore
.collection("users")
.doc(member)
.collection("groceries")
.snapshots())
.toList();
}));
return CombineLatestStream(list,
(values) => values.last as QuerySnapshot<Map<String, dynamic>>)
.asBroadcastStream();
// return StreamGroup.merge(list).asBroadcastStream();
}
as you can see iv tried a few ways to combine my streams and non workes
body:
body: TabBarView(children: [
_buildContent(
context,
db.getGroupStream(),
),
_buildContent(
context,
db.getPesonalStream(),
),
]),
my builder:
Widget _buildContent(BuildContext context, Stream<QuerySnapshot> stream) {
return StreamBuilder<QuerySnapshot>(
stream: stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
final docs = snapshot.data!.docs;
if (docs.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"The list is empty",
style: TextStyle(fontSize: 32, color: Colors.black54),
),
Text(
"Add a new item to get started",
style: TextStyle(fontSize: 16, color: Colors.black54),
),
],
));
}
int index = -1;
final cards = docs
.map((doc) => CrossableListTile(
doc, _showSetGroceryButtomSheetForm, index++))
.toList();
return Container(
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
child: ListView(
children: cards,
),
);
} else if (snapshot.hasError) {
return Center(
child: Column(
children: [Text("An error has occured while loading you'r list")],
),
);
}
return Center(
child: CircularProgressIndicator(
color: Colors.black,
));
},
);
}

Related

Display Firestore data as list without using a stream

I am trying to run a query on my Firebase Firestore data and display the results as a list. Typically, when I do this, I use a streambuilder, which updates the results whenever any data is changed. I was wondering how to display the same data only once, without subscribing to a stream. This is my code for the streambuilder, which updates on change.
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('announcements')
.orderBy('date', descending: true)
.snapshots(),
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshots) {
if (snapshots.connectionState == ConnectionState.active &&
snapshots.hasData) {
return ListView(
shrinkWrap: true,
physics: const BouncingScrollPhysics(),
children: snapshots.data!.size == 0
? [
const Padding(
padding: EdgeInsets.only(top: 10),
child: Text(
"You have no announcements",
style: TextStyle(
height: 1.0,
fontFamily: "urwmedium",
color: Color(0xffD5D6D7),
fontSize: 20,
),
),
),
]
: snapshots.data!.docs.map(
(DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Text(
data['announcementTitle'],
);
},
).toList(),
);
}
return Center(
child: CircularProgressIndicator(
color: CustomColors.textColor,
),
);
},
),
The above code works, but again, it loads information realtime. What would the code be if I wanted it to only load once at opening?
as the StreamBuilder is helpful in order to get stream of your collection snapshots
to get that data only once, use FutureBuilder instead:
FutureBuilder<QuerySnapshot>(
future: FirebaseFirestore.instance
.collection('announcements')
.orderBy('date', descending: true)
.get(),
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshots) {
if (snapshots.connectionState == ConnectionState.done &&
snapshots.hasData) {
return ListView(
shrinkWrap: true,
physics: const BouncingScrollPhysics(),
children: snapshots.data!.size == 0
? [
const Padding(
padding: EdgeInsets.only(top: 10),
child: Text(
"You have no announcements",
style: TextStyle(
height: 1.0,
fontFamily: "urwmedium",
color: Color(0xffD5D6D7),
fontSize: 20,
),
),
),
]
: snapshots.data!.docs.map(
(DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Text(
data['announcementTitle'],
);
},
).toList(),
);
}
return Center(
child: CircularProgressIndicator(
color: CustomColors.textColor,
),
);
},
),

flutter: how can I retrieve current user data from firebase firestore

am trying to retrieve current user data but it showing all the users data from firestore in app
here is my code
Widget build(BuildContext context){
CollectionReference users = FirebaseFirestore.instance.collection('users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(documentId).get(),
builder: ((context, snapshot){
if(snapshot.connectionState == ConnectionState.done){
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return Container(
child: Column(
children: [
Text('Email : '"${data['Email name'].toString()}",
style: const TextStyle(fontWeight: FontWeight.bold,wordSpacing: 10),),
const SizedBox(
width: 10,
),
Text('Contact : '"${data['age'].toString()}",
style: const TextStyle(fontWeight: FontWeight.bold,wordSpacing: 10),),
const SizedBox(
width: 10,
),
Text('Name : '"${data['firstname'].toString()}",
style: const TextStyle(fontWeight: FontWeight.bold,wordSpacing: 10),),
const SizedBox(
width: 10,
),
Text('Last Name : '"${data['lastname'].toString()}",
style: const TextStyle(fontWeight: FontWeight.bold,wordSpacing: 10),),
const SizedBox(
width: 10,
),
],
),
);
}
return const Text('Loading');
}),
here is documentId code
List<String> docIDs = [];
Future getDocId() async{
await FirebaseFirestore.instance.collection('users').get().then(
(snapShot) => snapShot.docs.forEach(
(documant){
print(documant.reference);
docIDs.add(documant.reference.id);
}));
}
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// ignore: prefer_const_constructors
Text('SignUp as ${user!.email!}',style: TextStyle(
fontSize: 20,
),),
Expanded(child: FutureBuilder(
future: getDocId(),
builder: (context, snapshot){
return ListView.builder(
itemCount: docIDs.length,
itemBuilder: (context, index){
return ListTile(
title: GetuserData(documentId: docIDs[index]),
);
},
);
},
))
when ever user post the form it submitted successfully and generate a post like summited form history but it showing to others users to but i wont it for only current user how to do that please any one can help me
You can use firestore query to return the current user using where()
on getDocId():
await FirebaseFirestore.instance.collection('users').get()
change to
await FirebaseFirestore.instance.collection('users')
.where('Email name', isEqualTo: 'myEmail#mail.test').get()
you can replace the email with your or consider using parameter to pass user email dynamically.

Flutter - Nested Streambuilder and FutureBuilder

I have categories collection with category name and id fields. In foods collection, the category is a reference field. I need to display like this:click to see expected output
where title is coming from categories collection and foods are coming from foods collection.
I tried using nested streambuilder: streambuilder 1: fetch categories in a listview streambuilder 2: fetch foods in a list. Inside streambuilder 2, i have used a futurebuilder to decode the category data. If category name in food and category name from streambuilder 1 is same, the food will be displayed under that category.+
class RestaurantDetails extends StatefulWidget {
final String id;
RestaurantDetails({required this.id, super.key});
#override
State<RestaurantDetails> createState() => _RestaurantDetailsState();
}
class _RestaurantDetailsState extends State<RestaurantDetails> {
List<FoodCategory> categories = [];
List<Food> foods = [];
#override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: StreamBuilder(
stream: getCategories(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
categories = snapshot.data!.docs
.map((item) => FoodCategory.fromMap(item))
.toList();
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: categories.length,
itemBuilder: ((context, cateindex) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 10),
child: Container(
height: 30,
width: MediaQuery.of(context).size.width * 1,
color: Colors.white,
child: Text(
categories[cateindex].name.toString(),
style: Theme.of(context)
.textTheme
.headline4!
.copyWith(fontSize: 20.0),
),
),
),
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('foods')
.doc(widget.id)
.collection('all')
.snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
foods = snapshot.data!.docs
.map((item) => Food.fromMap(item))
.toList();
return ListView.builder(
itemCount: foods.length,
physics:
const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemBuilder: ((context, foodindex) {
var catepath = foods[foodindex].cid!.path;
String cateDocumentName = catepath
.substring(11, catepath.length);
return Column(
children: [
FutureBuilder(
future: FirebaseFirestore.instance
.collection('categories')
.doc(cateDocumentName)
.get(),
builder: ((context,
AsyncSnapshot<
DocumentSnapshot>
snapshot) {
if (snapshot.hasData) {
Map<String, dynamic> data =
snapshot.data!.data()
as Map<String,
dynamic>;
if (data['name'] ==
categories[cateindex]
.name) {
return Padding(
padding: const EdgeInsets
.symmetric(
vertical: 10,
horizontal: 10),
child: Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius
.circular(
20),
color:
Colors.white),
height: 100,
width: MediaQuery.of(
context)
.size
.width *
1,
child: Row(
children: [
Image.network(
foods[foodindex]
.cover
.toString(),
height: 100,
width: 100,
errorBuilder:
((context,
error,
stackTrace) {
return Image
.asset(
'assets/images/food1.jpg',
height: 100,
width: 100,
);
}),
),
UIHelper
.horizontalSpaceMedium(),
Column(
crossAxisAlignment:
CrossAxisAlignment
.start,
children: [
UIHelper
.verticalSpaceSmall(),
Text(
foods[foodindex]
.name
.toString(),
style: Theme.of(
context)
.textTheme
.bodyText1,
),
UIHelper
.verticalSpaceSmall(),
Text(
'₹${foods[foodindex].price}',
style: Theme.of(
context)
.textTheme
.bodyText1!
.copyWith(
fontSize:
14.0),
),
UIHelper
.verticalSpaceMedium(),
],
)
],
)),
);
} else {
return const SizedBox();
}
} else {
return const CircularProgressIndicator
.adaptive();
}
}))
],
);
}));
} else {
return const CircularProgressIndicator
.adaptive();
}
},
)
],
);
}));
} else {
return const CircularProgressIndicator.adaptive();
}
}),
),
);
}
getCategories() {
return FirebaseFirestore.instance
.collection('categories')
.where('uid', isEqualTo: widget.id)
.snapshots();
}
}
categories data
click to see categories
food data
click to see food data
I get the output.see my output here but when data is large (i.e large number of foods inside a category) the app hangs. Is there anyother way we can achieve this? the data should load seamlessly regardless of data size.

Switch between streams flutter firebase

I have a class of complains in which there is a field of status. This status can be pending , inprogress, completed , rejected. In UI I have designed buttons to filter complaints on the basis of status.
The issue that I am facing is that when I switch stream on button action. It still contains the data of previous stream.
Can anyone help me to have 2 streams and not either of it contains data of previous stream.
bool _isSwitched = false;
List<Complains> complains = [];
final Stream<QuerySnapshot> _all = FirebaseFirestore.instance
.collection('Complains').orderBy("startTime", descending: true)
.snapshots();
final Stream<QuerySnapshot> _pending = FirebaseFirestore.instance
.collection('Complains').where('status', isEqualTo: 'Pending').orderBy("startTime", descending: true)
.snapshots();
ElevatedButton(onPressed:(){
setState(() {
_isSwitched = !_isSwitched;
complains.clear();
complains2.clear();
});
} , child: Text("Switch Stream"),),
StreamBuilder<QuerySnapshot>(
initialData: null,
stream: _isSwitched?_all:_pending,
builder: (context, snapshot) {
if (snapshot.hasError) {
Text("Error");
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
//length of stream is greater than 0
if (snapshot.data!.docs.length == 0) {
return Center(
child: Text("No Complains"),
);
}
for (var element in snapshot.data!.docs) {
Complains complain = Complains.fromMap(element.data() as Map<String, dynamic>);
_isSwitched?complains.add(complain):complains2.add(complain);
}
return ListView.builder(
itemBuilder: (context, index) {
return InkWell(
onTap: () {
},
child: Card(
margin: EdgeInsets.all(8.0),
elevation: 5,
color: Colors.white70,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0))),
child: Container(
padding: EdgeInsets.all(20),
child:
Column(children: [
Text("Title\n ${complains[index].title}",style: TextStyle(fontStyle: FontStyle.italic),),
Table(
children: [
TableRow(children: [
Text("Name: " ),
Text("Address: "+ complains[index].address.toString(),),
]),
TableRow(children: [
Text("Priority: "+ complains[index].priority.toString(),),
Text("Status: "+complains[index].status.toString(),),
]),
TableRow(children: [
Text("Worker: "+ complains[index].worker.toString(),),
Text("Service: "+ complains[index].service.toString(),),
]),
],
),
],)
),
),
);
},
itemCount: _isSwitched?complains2.length:complains.length,
);
},
),
),

FutureBuilder shows [instance] instead of actual data

I want to download a list from firestore and display as a list in flutter widget. The data is successfully downloaded (proved by the print(cp.data()).
However, the result shown is [Instance of '_JsonQueryDocumentSnapshot'] instead of the actual carpark data.
Could anyone pls help point out what the bug is.
Thanks
class DownloadDataScreen extends StatefulWidget {
#override
_DownloadDataScreen createState() => _DownloadDataScreen();
}
class _DownloadDataScreen extends State<DownloadDataScreen> {
List<DocumentSnapshot> carparkList = []; //List for storing carparks
void initState() {
super.initState();
readFromFirebase();
}
Future readFromFirebase() async {
// await FirebaseFirestore.instance
await FirebaseFirestore.instance
.collection('carpark')
.get()
.then((QuerySnapshot snapshot) {
snapshot.docs.forEach(
(DocumentSnapshot cp) {
carparkList.add(cp);
print('printing cp');
print(cp.data());
},
);
});
**return carparkList;**
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text(
'Car Park',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
centerTitle: true,
),
body: FutureBuilder(
future: readFromFirebase(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
List<Widget> children;
if (snapshot.hasData) {
children = <Widget>[
const Icon(
Icons.check_circle_outline,
color: Colors.green,
size: 60,
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Column(
children: [
Text('Result: ${snapshot.data}'),
],
),
)
];
} else if (snapshot.hasError) {
children = <Widget>[
const Icon(
Icons.error_outline,
color: Colors.red,
size: 60,
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Text('Error: ${snapshot.error}'),
)
];
} else {
children = const <Widget>[
SizedBox(
child: CircularProgressIndicator(),
width: 60,
height: 60,
),
Padding(
padding: EdgeInsets.only(top: 16),
child: Text('Awaiting result...'),
)
];
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: children,
),
);
},
),
);
}
}
First, you don't need to call the function from the init because you already use the FutureBuilder.
Also, you don't need to cast it because when the future completes, the async snapshot already would provide you a list of DocumentSnapshot in the data and the .doc propertie.
Like this:
FutureBuilder<QuerySnapshot>(
builder:(context,snapshot){
if(snapshot.hasData){
/// here your data
snapshot.data.docs;
}
)