Reputation: 974
I have a few questions (I am new to Dart) w.r.t the code below which I found. What I am trying to achieve: 1) Write more than one line in the brackets for errors. But I get error saying 'set literals were supported upto 2.2....'. It would also be good to know the list of possible errors so I can alert the user accordingly.
2) Increment a counter pertaining to number of documents and keep storing document id's in one variable, by overwriting the last value if more than one document is found. I am trying to check that there should only be one document and take action if counter goes to 2.
void _checkIfAdmin() {
Firestore.instance
.collection('Admin')
.where('Email', isEqualTo: widget._user.email)
.snapshots()
.handleError((error)=>{
//
})
.listen((data) => data.documents.forEach((doc) => gpCounter+=1)); // Store document id?
print('Counter: $gpCounter');
}
Here I am getting printed gpCounter=0 when I know there are two documents in there. I tried adding async/await in the function but it seems the firebase statement does not return any future so I get a warning to use await only for futures.
Upvotes: 1
Views: 624
Reputation: 126574
I will try to address as many issues as I could understand in your question. If you have any other issues, you should probably ask another question.
Set literals: the syntax for anonymous functions is () {}
and not () => {}
.
() =>
is a shorthand syntax to return the value of a single expressions and in your case, you are returning a Set
in your anonymous functions because {}
create Set
's (or Map
's if you have colons in there).
The solution for this is (error) { ... }
.
You print your gpCounter
value synchronously, however, you are listening to a Stream
.
Because of that, you should move your print
statement to your listen
function:
.listen(data) {
print('Counter: ${data.documents.length}');
})
Upvotes: 1