here is the firebase firestore section
how do I read the value 'purchase-id'
this is how I did, but not working
var collection = FirebaseFirestore.instance.collection('users');
final docSnapshot = await collection.doc('$index').get();
Map<String, dynamic> data = docSnapshot.data()!;
final purID = data['purchased-id'];
the value is receiving but as the future value
here is how the value is receiving
final purchaseID = coursePurchaseCheck
but this is a Future value, how do I parse that to normal data
how do I properly get document value from firebase??
Check your index is equal to document key ??
You can get document key with below code:
List<String> _userKey = [];
await fireStore
.collection('user').get()
.then((QuerySnapshot querySnapshot) {
for (var doc in querySnapshot.docs) {
_userKey.add(doc.id);
}
});
And get data below:
for(String _key in _userKey){
await FirebaseFirestore.instance
.collection('user')
.doc(_key)
.get()
.then((DocumentSnapshot documentSnapshot) async {
if (documentSnapshot.exists) {
var data = documentSnapshot.data();
var res = data as Map<String, dynamic>;
final purID = res['purchased-id'];
}
});
}
Related
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()
I want to retrieve data from firebase via "continent" value
here is my database
this is my code:
DatabaseReference ref = FirebaseDatabase.instance
.reference()
.child("Country/")
.orderByChild("continent")
.equalTo("Asia");
ref.once()
.then((DataSnapshot datasnapshot) {
datalist.clear();
var keys=datasnapshot.value.keys;
var values=datasnapshot.value;
for(var key in keys) {
Country data = new Country(
values[key]['Country'],
values[key]['continent'],
values[key]['Capital'],
);
}
datalist.add(data);
}
what is wrong?
DatabaseReference ref = FirebaseDatabase.instance
.reference()
.child("Country/")
.orderByChild("continent")
.equalTo("Asia");
ref.once()
.then((DataSnapshot snapshot) {
Map<dynamic, dynamic> values = snapshot.value;
values.forEach((key, values) {
print(values["continent"]);
});
});
From the 9.0.0 version of firebase_database, many breaking changes applied. Now its something like:
Query ref = FirebaseDatabase.instance.ref()
.child("Country/")
.orderByChild("continent")
.equalTo("Asia");
ref.once()
.then((DatabaseEvent event) {
if(event.snapshot.value == null) return;
var values = event.snapshot.value;
print(values);
});
I need to search for data from a map in the "users" collection of a single document. But I only found ways to search all documents.
See below.
Code 1:
FirebaseFirestore.instance
.collection('users')
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) {
Map<String, dynamic> res = doc["favorite"];
});
});
Code 2:
final String _collection = 'users';
final FirebaseFirestore _fireStore = FirebaseFirestore.instance;
getData() async {
return await _fireStore.collection(_collection).get();
}
getData().then((val) {
if (val.docs.length > 0) {
print(val.docs[0].data()["favorite"]);
} else {
print("Not Found");
}
});
Hello you can search in fields:
// Create a reference to the cities collection
var citiesRef = db.collection("cities");
// Create a query against the collection.
var query = citiesRef.where("state", "==", "CA");
See the Firestore queries reference here:
https://firebase.google.com/docs/firestore/query-data/queries
Trying to check firebase record and perform subsequent logics,
Future<bool> isAlreadyThere(selectedPropertyId) async {
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
var myMapQuery = Firestore.instance
.collection("props")
.where('xxx', isEqualTo: xxxid)
.where('yyy', isEqualTo: user.uid);
var querySnapshot= await myMapQuery.getDocuments();
var totalEquals= querySnapshot.documents.length;
return totalEquals > 0;
}
and in the onTap() of widget ,
bool isThere=isAlreadyThere(suggestion.documentID) as bool;
if (isThere) {//do stuff here}
errors,
type 'Future' is not a subtype of type 'bool' in type cast
I know its the casting , but tried removing and some other ways as well , wont work.
await is missing in where the query
Future<bool> isAlreadyThere(selectedPropertyId) async {
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
var myMapQuery = (await Firestore.instance
.collection("props")
.where('xxx', isEqualTo: xxxid)
.where('yyy', isEqualTo: user.uid));
var querySnapshot= await myMapQuery.getDocuments();
var totalEquals= querySnapshot.documents.length;
return totalEquals > 0;
}
Use method like
isAlreadyThere(suggestion.documentID).then((value) {
if (value) {//do stuff here}
});
The data from the cloud_firestore database is in the form of JSON. However, how to transform the data from JSON in a List of Map?
The dummy data in my firestore
Data to List of Map:
final CollectionReference ref = Firestore.instance.collection('food');
List<Map<String, dynamic>> listOfMaps = [];
await ref.getDocuments().then((QuerySnapshot snapshot) {
listOfMaps =
snapshot.documents.map((DocumentSnapshot documentSnapshot) {
return documentSnapshot.data;
}).toList();
});
print(listOfMaps);
Just in case if You want to use better way. Parse data to List of Objects:
1) create a model class:
class Food {
String affordability;
String title;
Food.fromJson(Map<String, dynamic> jsonData) {
this.affordability = jsonData['affordability'];
this.title = jsonData['title'];
}
}
2) convert to list of Food:
final CollectionReference ref = Firestore.instance.collection('food');
List<Food> list = [];
await ref.getDocuments().then((QuerySnapshot snapshot) {
list = snapshot.documents.map((DocumentSnapshot documentSnapshot) {
return Food.fromJson(documentSnapshot.data);
}).toList();
});
print(list);