Merging multiple firebase stream in flutter - firebase

Guy this is a real problem merging multiple firebase streams to one stream. Someone should write an article or a simple video tutorial on this. Either using StreamGroup, FlatMap(), Rx.combineLatest, StreamZip or CombineLatestesStream. I have tried solving this since yesterday and I cant get a clear guidance.
class CartPage extends StatefulWidget{
#override
_CartPageState createState() => _CartPageState();
}
class _CartPageState extends State<CartPage> {
// a firebase collection for all items
Stream stream1 = EcommerceApp.firestore
.collection("items")
.where("shortInfo",
whereIn: EcommerceApp.sharedPreferences
.getStringList(EcommerceApp.userCartList))
.snapshots();
// a firebase collection for flash sales items
Stream stream2 = EcommerceApp.firestore
.collection("flashitem")
.where("shortInfo",
whereIn: EcommerceApp.sharedPreferences
.getStringList(EcommerceApp.userCartList))
.snapshots();
List<QuerySnapshot> getList(QuerySnapshot list1) {
List<QuerySnapshot> result = [];
(list1 as List).forEach((element) {
result.add(element);
});
return result;
}
#override
Widget build(BuildContext context) {
Stream combineStream = Rx.combineLatest2(streamA, streamB, (a, b) => [a, b]);
return Scaffold(
appBar: MyAppBar(),
body:CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Container(
height: 10.0,
),
),
StreamBulder(
stream: combineStream,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return SliverToBoxAdapter(
child: Center(
child: circularProgressBar(),
),
);
} else {
List<QuerySnapshot> _list = [];
_list.addAll(getList(snapshot.data[0]));
_list.addAll(getList(snapshot.data[1]));
if (_list.length == 0) {
} else {
return SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
ProductModel model = ProductModel.fromJson(
_list[index].docs[index].data());
return cartSourceInfo(model, context,
removeCartFunction: () =>
removeItemFromUserCart(model.shortInfo));
},
childCount: childCount: snapshot.hasData ? _list.length : 0,
),
);
}
}
}
)
);
}
}
Majority of the answers here are using Observable library which is deplecated in rxdart, and when am trying to use the same syntax to solve using Rx.latestCombine2 there is no data streamed. and when I try to pass a querySnapshot of type list to a stream Stream<List> I am getting a batch of errors:
Class 'List' has no instance getter 'docs'.
Receiver: Instance(length:2) of '_GrowableList'
Tried calling: docs
Please show me how I can either nest these two firebase stream into ome or how I can use Rx.combineLatest2 method to solve this problem.

The syntax looks correct but when trying to access data of each stream you have to access it by index since the snapshot is basically a list
so to access snapshot of stream1 and stream2 it should be accesssed like this
snapshot.data[0].docs and snapshot.data[1].docs respectively.
You can combine both the streams and show the list in the Ui, And make sure to assign a appropriate type T based on the type of snapshot.data[index].docs
List<QuerySnapshot> combineLists(
List<QuerySnapshot> list1, List<QuerySnapshot> list2) {
List<QuerySnapshot> result = [];
list1.forEach((element) {
result.add(element);
});
list2.forEach((element) {
result.add(element);
});
return result;
}
StreamBulder(
​stream: combineStream,
​builder: (context, AsyncSnapshot<List<QuerySnapshot>> snapshot) {
​if (!snapshot.hasData) {
​return SliverToBoxAdapter(
​child: Center(
​child: circularProgressBar(),
​),
​);
​} else {​
final List<QuerySnapshot> _list=[];
final List<QuerySnapshot> combineSnapshot =
combineLists(snapshot.data[0], snapshot.data[1]);
​if (_list.length == 0) {
​return addItems();
​} else {
return SliverList(
​delegate: SliverChildBuilderDelegate(
(context, index) {
ProductModel model = ProductModel.fromJson(
_list[index].data());
return cartSourceInfo(model, context, removeCartFunction:
() => removeItemFromUserCart(model.shortInfo));
​},
childCount:_list.length,
),
);
​}
}
}

Related

Bad state: Snapshot has neither data nor error in flutter when using StreamBuilder and Firestore

I'm adding data from Firestore to a Stream from StreamBuilder, but I'm getting the following error:
Exception has occurred. StateError (Bad state: Snapshot has neither data nor error
My code.
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
AppState? estado;
static String? userID = FirebaseAuth.instance.currentUser?.uid;
static final userColeccion = FirebaseFirestore.instance.collection("users");
var groupfav = ' ';
Stream<QuerySnapshot>? taskGroup;
#override
void initState() {
super.initState();
getGroupFavData();
}
void getGroupFavData() async {
var groupFavData = await userColeccion.doc("$userID").get();
var groupfav = groupFavData.data()!['groupfav'];
taskGroup = FirebaseFirestore.instance
.collection("groups")
.doc(groupfav) // pass the obtained value
.collection("task")
.snapshots();
}
#override
Widget build(BuildContext context) {
estado = Provider.of<AppState>(context, listen: true);
return Scaffold(
appBar: AppBar(
title: const Text("Home"),
automaticallyImplyLeading: false,
),
body: StreamBuilder(
stream: taskGroup,
builder: (
BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot,
) {
if (snapshot.hasError) {
return const Text("error");
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Loading");
}
var data = snapshot.requireData;
return ListView.builder(
itemCount: data.size,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text("${data.docs[index]['titulo']}"),
subtitle: Text("${data.docs[index]['contenido']}"),
onTap: () {},
trailing: IconButton(
icon: const Icon(Icons.delete),
color: Colors.red[200],
onPressed: () {},
),
),
);
},
);
},
),
);
}
}
Ok, looking at your issue, I see that 1) you need to get the data of the document BEFORE you start listening on that document, which is normal, so you want to do a call first to the collection, get the document, then listen on the document's collection called task, which makes sense. Your issue is still an asynchronous issue. The app is rebuilding on a stream that still hasn't arrived; you have to fix the sequence of things.
You then need to switch things up a bit and do the following:
Option #1:
a) Use a FutureBuilder: this will allow you to make the async call to get the document name based on the user Id
b) After you get the document associated to that user, you want to listen on the stream produced by the collection called tasks in that document. There is where then you can hook up the StreamBuilder.
Option #2:
a) Keep things the way you have, but do a listen on the taskGroup snapshots; but keep rebuilding the list as the values arrive on that collection.
Those are my suggestions.
Here's some brief code on option 1:
// .. in your Scaffold's body:
Scaffold(
body: FutureBuilder( // the future builder fetches the initial data
future: userColeccion.doc("$userID").get(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
var groupfav = snapshot.data()!['groupfav'];
// then once the 'groupfav' has arrived,
// start listening on the taskGroup
taskGroup = FirebaseFirestore.instance
.collection("groups")
.doc(groupfav) // pass the obtained value
.collection("task")
.snapshots();
return StreamBuilder(
stream: taskGroup,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
// the rest of your code
});
}
return CircularProgressIndicator();
}
)
)
Option 2 would be something like:
List<Task> userTasks = [];
void getGroupFavData() async {
var groupFavData = await userColeccion.doc("$userID").get();
var groupfav = groupFavData.data()!['groupfav'];
taskGroup = FirebaseFirestore.instance
.collection("groups")
.doc(groupfav) // pass the obtained value
.collection("task")
.snapshots().listen((snapshot) {
// here populate a list of your tasks
// and trigger a widget rebuild once you've grabbed the values
// and display it as a list on the UI
setState(() {
userTasks = snapshot.docs.map((d) => Task.fromJson(d.data())).toList();
});
});
}
And in your Scaffold, you can have a ListView just rendering the items on that task list, like:
ListView.builder(
itemCount: userTasks.length,
itemBuilder: (context, index) {
// render your tasks here
})
Here's a Gist with some working code to illustrate my point. Run it on DartPad and you'll see how using a FutureBuilder wrapping a StreamBuilder will accomplish what you want.
If you run the above code on DartPad, you'll get the following output:
Hope those pointers take you somewhere.

Getting an error when trying to retrieve data from Firebase FireStore document

I'm trying to retrieve all the courses that the user has enrolled in, these courses are present in an array within the document.
After retrieving the course ID from the users collection, I'm trying to retrieve the course details from the courses collection.
But before the courses variable is populated, the coursesCollection statement is executed and throwing the below error.
======== Exception caught by widgets library =======================================================
The following assertion was thrown building _BodyBuilder:
'in' filters require a non-empty [List].
'package:cloud_firestore/src/query.dart':
Failed assertion: line 706 pos 11: '(value as List).isNotEmpty'
Here is the error causing code:
List courses = [];
var coursesCollection;
void fetchCourses() async {
final loggedInUser = FirebaseAuth.instance.currentUser;
if (loggedInUser != null) {
final userCollection = await FirebaseFirestore.instance.collection('users').doc(loggedInUser.uid).get();
courses = userCollection.get('coursesEnrolled');
}
}
#override
void initState() {
fetchCourses();
coursesCollection = FirebaseFirestore.instance.collection('courses').where('courseID', whereIn: courses);
super.initState();
}
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: coursesCollection.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(backgroundColor: kBrandColor),
);
}
}
final courseListStream = snapshot.data!.docs.map((course) {
return CourseData.fromDocument(course);
}).toList();
List<BadgedCourseCard> courseCards = [];
for (var course in courseListStream) {
final courseDocID = course.courseDocID;
final courseID = course.courseID;
final courseTitle = course.courseTitle;
final courseImage = course.courseImage;
final courseBgColor = hexToColor(course.courseBackgroundColor.toString());
hexToColor(course.courseFgColor.toString());
final badgedCourseCard = BadgedCourseCard(
courseTitle: courseTitle.toString(),
courseTitleTextColor: courseFgColor,
cardBackgroundColor: courseBgColor,
courseImage: courseImage.toString(),
courseCardTapped: () {
Provider.of<CourseProvider>(context, listen: false).currentCourseDetails(
currentCourseDocID: courseDocID,
currentCourseID: courseID,
);
Navigator.of(context).push(ScaledAnimationPageRoute(CourseLandingPage(courseID: courseID.toString())));
},
courseBookmarkTapped: () => print("Course Bookmark Tapped"),
rightPadding: 3,
bottomPadding: 0.5,
cardWidth: 80,
);
courseCards.add(badgedCourseCard);
}
return SizedBox(
height: 20.5.h,
child: ListView(
physics: BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
children: courseCards,
),
);
},
);
}
How can I fix this issue?
Here,
coursesCollection = FirebaseFirestore.instance.collection('courses').where('courseID', whereIn: courses);
courses would be [] because fetchCourses is an async call.
Change the return type of fetchCourses from void to Future<void> & try using a then callback:
#override
void initState() {
super.initState();
fetchCourses().then((val) {
coursesCollection = FirebaseFirestore.instance.collection('courses').where('courseID', whereIn: courses);
setState(() {});
});
}
I would also recommend to use FutureBuilder as a better alternative.
coursesCollection is null that's why you're getting another error. Render StreamBuilder only when coursesCollection is not null.
coursesCollection != null ? StreamBuilder(...) : SizedBox(),
For listening to the user's enrolled courses, another StreamBuilder can be used. It would be a nested StreamBuilder setup.
StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
stream: FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser.uid).snapshots(),
builder: (context, snapshot) => snapshot.hasData ? StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
stream: FirebaseFirestore.instance.collection('courses').where('courseID', whereIn: snapshot.data!.data()!['coursesEnrolled']).snapshots(),
builder: (context, snapshotTwo) {},
) : Text('Loading...'),
),

my Stream is not updating when isTeacher() function is called

what I am trying to achieve is load data from a specific collection(teacher) of my database. So I am using a function called isTeacher(). which checks if the current user's uid belongs in that collection. if not then it is a student. it stores the value in a string called value. so when I am using stream builder to load data available in their specific collection or documents, my stream builder shows circular progress and after that, it doesn't load the data. Any help is appreciated.
Thank you
`class MyClasses extends StatefulWidget {
#override
_MyClasses createState() => _MyClasses();
}
String value;
String classPassword;
List<dynamic> catchUserDetails = [];
class _MyClasses extends State<MyClasses> {
Future isTeacher() {
return FirebaseFirestore.instance
.collection('teacher')
.doc(FirebaseAuth.instance.currentUser.uid)
.get()
.then((DocumentSnapshot doc) {
value = doc.exists.toString();
print(doc.data());
print(value);
print('isteacher called in method');
});
}
#override
Widget build(BuildContext context) {
isTeacher();
return Scaffold(
body: SafeArea(
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection(value)
.doc(FirebaseAuth.instance.currentUser.uid)
.collection('class')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
} else {
final messages = snapshot.data.documents.reversed;
List<GenerateClass> messageBubbles = [];
for (var message in messages) {
final messageText = message.data()['className'];
final messageBubble = GenerateClass(
classID: messageText,
//nnouncementID: i,
);
messageBubbles.add(messageBubble);
}
return ListView(
//itemExtent: 100,
children: messageBubbles,
);
}
},
),
),
);`
Solved it by using a FutureBuilder
FutureBuilder(
future: isTeacher(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return StreamBuilder();

Firestore collection query as stream in flutter

I'm trying to query a few documents from a collection, this query should listen to changes made in the queried documents, so I'd need a stream. I'm doing following (in Dart/Flutter)
Stream<List<MatchRequest>> _getNewMatches() {
return Collection<MatchRequest>(path: 'requests')
.ref
.where('status', isNull: true)
.where('users', arrayContains: ['$currentUid'])
.orderBy('last_activity')
.snapshots()
.map((list) => list.documents.map(
(doc) => Global.models[MatchRequest](doc.data) as MatchRequest));
}
(The object Collection sets the path to the ref in it's constructor, eg: ref = db.collection($path) and the map makes a model of the results)
Then I'm using a StreamBuilder with stream invoking the method above and builder checking if snapshot.hasData. But it keeps loading, snapshot.hasData keeps being false. What am I doing wrong here?
EDIT:
My firestore security rules contain:
match /requests/{requestId} {
allow read: if isLoggedIn();
allow write: if isLoggedIn();
}
When removing every where and orderBy, it doesn't find anything as well. And there are documents present in the requests-collection
When trying to query only 1 document as a stream from the requests-collection, he does find the result
Is it because I should add indexes to my firestore indexes? But this won't solve my first problem which is that even without where and orderBy, it doesn't get any data
I've written a simple example of it seems to be like what you are trying to do but are missing the listen() method:
Firestore.instance.collection('collection')
.where('field', isEqualTo: 'value')
.orderBy('field')
.snapshots()
.listen((QuerySnapshot querySnapshot){
querySnapshot.documents.forEach((document) => print(document));
}
);
This is just an example of how you can take the data from a Firestore Stream and use it on a StreamBuilder:
class _MyHomePageState extends State<MyHomePage> {
Stream dataList;
#override
void initState() {
dataList = Firestore.instance.collection('collection')
.where('field', isEqualTo: 'value')
.orderBy('field')
.snapshots();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: StreamBuilder(
stream: dataList,
builder: (context, asyncSnapshot) {
if(asyncSnapshot.hasError)
return Text('Error: ${asyncSnapshot.error}');
switch (asyncSnapshot.connectionState) {
case ConnectionState.none: return Text('No data');
case ConnectionState.waiting: return Text('Awaiting...');
case ConnectionState.active:
return ListView(
children: asyncSnapshot.data.map((document) => Text(document['value'])),
);
break;
case ConnectionState.done: return ListView(
children: asyncSnapshot.data.map((document) => Text(document['value'])),
);
break;
}
return null;
}),
),
);
}
}

Flutter how to get data from async function

I am having extreme difficulty in handling this. I call an async function that will get some info from SQLite but I can't seem to get it. It just renders a empty screen in which should be a listview.
List allItems = new List();
Future<void> pegaDados() async {
var itens = await geraCardapio();
for (var i = 0; i < itens.length; i++) {
print((itens[i].toMap()));
allItems.add(itens[i].toMap());
}
}
print(pegaDados());
return ListView.builder(
itemCount: allItems.length,
itemBuilder: (context, index) {
return ListTile(
leading: Image.asset("assets/"+ allItems[index]['imagem'], fit: BoxFit.contain,),
title: Text(allItems[index]['pedido']),
trailing: Text(allItems[index]['valor']),
);
},
);
Thank you very much.
I managed to get the solution, thanks to both people who answered the question (using both solutions I managed to get this little frankenstein)
Future<dynamic> pegaDados() async {
var allItems = await geraCardapio();
return allItems.map((allItems) => allItems.toMap());
}
return FutureBuilder(
future: pegaDados(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
print(snapshot.data);
var objeto = [];
for (var i in snapshot.data) {
objeto.add(i);
}
print(objeto);
return Container(
child: ListView.builder(
itemCount: objeto.length,
itemBuilder: (context, index) {
return ListTile(
leading: Image.asset("assets/"+ objeto[index]['imagem'], fit: BoxFit.contain),
title: Text(objeto[index]['pedido']),
trailing: Text(objeto[index]['valor'].toString()),
);
},
),
);
} else if (snapshot.hasError) {
throw snapshot.error;
} else {
return Center(child: CircularProgressIndicator());
}
});
thanks to [Mohammad Assem Nasser][1] and [Eliya Cohen][2] for the help!
[1]: https://stackoverflow.com/users/11542171/mohammad-assem-nasser
[2]: https://stackoverflow.com/users/1860540/eliya-cohen
You should first understand what is Future operations (Future function in your case). Future operations are the operations which take time to perform and return the result later. To handle this problem, we use Asynchronous functions.
Asynchronous Function let your program continue other operations while the current operation is being performed. Dart uses Future objects (Futures) to represent the results of asynchronous operations. To handle these operations, we can use async/await, but it is not possible to integrate async and await on widgets. So it is quite tricky to handle futures in widgets. To solve this problem flutter provided a widget called FutureBuilder.
In FutureBuilder, it calls the Future function to wait for the result, and as soon as it produces the result it calls the builder function where we build the widget.
Here is how it should be:
class Home extends StatefulWidget {
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List allItems = new List();
Future<List> pegaDados() async{
var items = await geraCardapio(); // TODO: Add this function to this class
for (var i = 0; i < items.length; i++) {
print((items[i].toMap()));
allItems.add(items[i].toMap());
}
return items;
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: Text('Demo')),
body: FutureBuilder(
future: pegaDados(),
builder: (context, snapshot){
if(snapshot.connectionState == ConnectionState.done){
return Container(
child: ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return ListTile(
leading: Image.asset("assets/"+ snapshot.data[index]['imagem'], fit: BoxFit.contain,),
title: Text(snapshot.data[index]['pedido']),
trailing: Text(snapshot.data[index]['valor']),
);
},
),
);
}
else if(snapshot.hasError){
throw snapshot.error;
}
else{
return Center(child: CircularProgressIndicator());
}
},
),
);
}
}
Here is the link to a short video that will explain FutureBuilder in a concise way.
I'm not sure how your widget tree looks like, but I'm assuming ListView is being built simultaneously with pegaDados. What you're looking for is a FutureBuilder:
Future<dynamic> pegaDados() async{
var items = await geraCardapio();
return items.map((item) => item.toMap());
}
...
FutureBuilder<dynamic>(
future: pegaDados(),
builder: (BuilderContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Uninitialized');
case ConnectionState.active:
case ConnectionState.waiting:
return Text('Awaiting result...');
case ConnectionState.done:
if (snapshot.hasError)
throw snapshot.error;
//
// Place here your ListView.
//
}
return null; // unreachable
}

Resources