Retrieve Document content from Firebase in Flutter - firebase

I'm trying to retrieve user data from a Firebase Collection.
This is what it looks like:
This is the method I wrote to get the data:
static String getUserData(creatorId, keyword) {
var documentName = Firestore.instance
.collection('users')
.document(creatorId)
.get()
.then((DocumentSnapshot) {
String data = (DocumentSnapshot.data['$keyword'].toString());
return data;
});
}
The method only returns null. If I print the String in the Method it works. How can I return the String?
Help would be greatly appreciated.
Cheers Paul

You need to use async and await to be able to wait for the data to be fully retrieved and then you can return the data.
The async and await keywords provide a declarative way to define asynchronous functions and use their results.
For example:
Future<String> getUserData(creatorId, keyword) async {
var documentName = await Firestore.instance
.collection('users')
.document(creatorId)
.get()
.then((DocumentSnapshot) {
String data = (DocumentSnapshot.data['$keyword'].toString());
return data;
});
}
And then you since getUserData returns a Future, you can use the await keyword to call it:
await getUserData(id, key);
https://dart.dev/codelabs/async-await#working-with-futures-async-and-await

Related

Pass variable from function to late stream flutter

I am trying to get getGrupoFav to pass it as a variable to the late Stream<QuerySnapshot> task..., I tried with get but I did not know how to do it and I did not find a solution, I do not know if there is a better way to do it.
the error says
"Try correcting the name to the name of an existing getter, or defining a getter or field named 'getGrupoFav'.
.doc(getGrupoFav)
"
_fetch() async {
final String? userID = FirebaseAuth.instance.currentUser?.uid;
await FirebaseFirestore.instance
.collection("usuarios")
.doc("$userID")
.get()
.then((value) {
String getGrupoFav = value.data()!["grupofav"];
return getGrupoFav;
}).catchError((e) {
print(e);
});
}
late Stream<QuerySnapshot> task = FirebaseFirestore.instance
.collection("grupos")
.doc(getGrupoFav)
.collection("tareas")
.snapshots();
You should build your code something around like below and for the Flutter code syntax please have a look at this documentation
var collection = FirebaseFirestore.instance.collection('usarios');
var userID = FirebaseAuth.instance.currentUser?.uid;
var docSnapshot = await collection.doc(userID).get();
if (docSnapshot.exists) {
Map<String, dynamic> data = docSnapshot.data()!;
var name = data['name'];
}
Then you pass this variable to the document like,
var task = FirebaseFirestore.instance .collection("grupos") .doc(name).snapshots()

Flutter Firestore Update Where

I'm trying to run a query that retrieves a single row given a where clause and updates it. I understand that Firebase doesn't support an UpdateWhere operations so I'm trying to use a Transaction instead.
I'm having difficulty making it work, maybe I'm too used to sql dbs... Here's my broken code
try {
final whereQuery = _db
.doc(userPath(user))
.collection("someInnerCollection")
.where("active", isEqualTo: true)
.limit(1);
await _db.runTransaction((transaction) async {
final entry = await transaction.get(whereQuery); // This doesn't compile as .get doesn't take in a query
await transaction.update(entry, {
"someValue": "newValue",
});
});
} catch (e) {
...
}
From the test I’ve made, I would suggest the following to achieve what you mention:
Based on the following answer:
As you can see from the API documentation, where() returns a Query object. It's not a DocumentReference.
Even if you think that a query will only return one document, you still have to write code to deal with the fact that it could return zero or more documents in a QuerySnapshot object. I suggest reviewing the documentation on queries to see examples.
After doing the query consult, you have to get the DocumentReference for that given result.
Then, you can use that reference to update the field inside a Batched writes
try {
final post = await firestore
.collection('someInnerCollection')
.where('active', isEqualTo: true)
.limit(1)
.get()
.then((QuerySnapshot snapshot) {
//Here we get the document reference and return to the post variable.
return snapshot.docs[0].reference;
});
var batch = firestore.batch();
//Updates the field value, using post as document reference
batch.update(post, { 'someValue': 'newValue' });
batch.commit();
} catch (e) {
print(e);
}
You are passing the DocumentSnapshot back in the update() operation instead of DocumentReference itself. Try refactoring the like this:
final docRefToUpdate = _db.collection("colName").doc("docId");
await _db.runTransaction((transaction) async {
final entry = await transaction.get() // <-- DocRef of document to update in get() here
await transaction.update(docRefToUpdate, {
// Pass the DocumentReference here ^^
"someValue": "newValue",
});
});
You can use a collection reference and then update single fields using .update().
final CollectionReference collectionReference = FirebaseFirestore.instance.collection('users');
await collectionReference.doc(user.uid).collection('yourNewCollection').doc('yourDocumentInsideNestedCollection').update({
'singleField': 'whatever you want,
});
Same code using "where"
collectionReference.doc(user.uid).collection('yourNewCollection').doc().where('singleField', isEqualTo: yourValue).update({
'singleField': 'whatever you want,
});

Flutter :How to pass List to Firestore where condetion

How to pass a List as param to firestore where condition? To get all data meets the condition
ie:-
List<String> topicList ;
getData() async {
SharedPreferences pref = await SharedPreferences.getInstance();
topicList = pref.getStringList("topicSymbol");
}
Stream<QuerySnapshot> getStreamQuery(String collPath) {
List<String> filters ;
return db
.collection('$collPath')
.where('Symbol', isEqualTo: getData()) // <---- the problem is here
.snapshots();
}
Thank you in advance
There are a few problems with this approach: firstly getData is an async function and you are not awaiting to get the return list from get data. You want to call await getData() before the return call in getStreamQuery.
Secondly, you should use .where('Symbol', isEqualTo: topicList) in order to compare the lists (this is because getData() returns null)
Let me know if this works for you!
Since you're using await in there, your getData() actually returns a Future - and you're failing to deal with that when calling it.
The simplest fix is to make getStreamQuery async too:
getData() async {
SharedPreferences pref = await SharedPreferences.getInstance();
return pref.getStringList("topicSymbol");
}
Future<Stream<QuerySnapshot>> getStreamQuery(String collPath) async {
List<String> filters ;
return db
.collection('$collPath')
.where('Symbol', isEqualTo: await getData())
.snapshots();
}
When you call getStreamQuery, you'll need to use await there tool.

Future Function Returning Null Firestore

I am trying to make a function in order to see which method the user has used to sign in eg. Google. I made a function which was called getProvider(). Here is the code for the function.
Future<String> getProvider (FirebaseAuth _auth)
async {
var user = await _auth.currentUser();
var provider;
Firestore firestore = Firestore.instance;
await firestore
.collection('Users')
.document('${user.uid}')
.get()
.then((value) {
provider =
value.data['Authentication Provider'];
return provider;
});
}
However, when I print the output value of the function outside the function itself, I get null.
print(await getProvider(_auth)) //This prints null
However, when I print the value of provider inside the function, it is not null and In this case, I get google. When I removed this function and called all of the code wherever I needed it, it worked as expected.
This shows that there is a problem with the return value of this getProvider() function. Can someone please let me know how I can fix this so that it actually returns the correct value?
Try this:
Future<String> getProvider(FirebaseAuth _auth) async {
var user = await _auth.currentUser();
Firestore firestore = Firestore.instance;
return firestore
.collection('Users')
.document('${user.uid}')
.get()
.then((value) => value.data['Authentication Provider']);
}
then() returns a Future which you can later await on to get the value. Since your method are already returning Future<String> you should be able to just return the Future generated by then().

Querying firestore to check if field value exist and Converting Stream into Future?

I have a function which i created to query firestore and checkwhether a phoneNo exist in the collection called 'users'
I want to get a boolean if the phone No already exist in a document field.
How can i do it
Future<bool> phoneRegisterCheck(phone) async{
bool phoneAlreadyRegistered;
print('start');
var result = Firebase.instance.collection('users').where('phoneNo', isEqualTo: phone);
}
I want to wait for the query to finish and then return the result.
When i Use listen method it is not happening right function returns null.
Future<bool> phoneRegisterCheck(phone) async{
bool phoneRegistered;
print('start');
var result = DatabaseService().userCollection.where('phoneNo', isEqualTo: phone);
result.snapshots().listen((val){
val.documents.isEmpty ? phoneRegistered=false:phoneRegistered=true;
});
return phoneRegistered;
}
Since streams are asynchronous then in your code the return statement will be executed before the data is fully retrieved therefore you get null. You should
use await for instead of listen:
Future<bool> phoneRegisterCheck(phone) async{
bool phoneRegistered;
print('start');
var result = DatabaseService().userCollection.where('phoneNo', isEqualTo: phone).snapshots();
await for(var values in result){
values.documents.isEmpty ? phoneRegistered=false:phoneRegistered=true;
}
return phoneRegistered;
}
From the docs:
Streams can be created in many ways, which is a topic for another article, but they can all be used in the same way: the asynchronous for loop (commonly just called await for) iterates over the events of a stream like the for loop iterates over an Iterable.
https://dart.dev/tutorials/language/streams

Resources