i want to use "docid" outside the function how to do it ?
getC() async {
Usersref.where("email", isEqualTo: widget.list['email'])
.get()
.then((QuerySnapshot snapshot) {
snapshot.docs.forEach((document) {
print(document.id);
final docid = document.id;
});
});
}
Future<List<String>>getC() async {
List<String> idList=[];
await Usersref.where("email", isEqualTo: widget.list['email'])
.get()
.then((QuerySnapshot snapshot) {
snapshot.docs.forEach((document) {
print(document.id);
idList.add( document.id);
});
});
return idList;
}
Related
I'm new to flutter. This is the method that I used to retrieve the data from firebase and I'm able to get the exact answer in the console. My question is how I can convert this code into future builder so I am able to read the data in my application.
void getUser() async {
firestoreInstance.collection("User Data").get().then((querysnapshot) {
querysnapshot.docs.forEach((result) {
firestoreInstance
.collection("User Data")
.doc(result.id)
.collection("bank")
.where('account_username', isEqualTo: ownerData?.name2)
.get()
.then((query Snapshot) {
querysnapshot.docs.forEach((result) {
print (result["bank_name"]);
});
});
});
});
}
You should return the value from the query, not print it
Your function should look like this
Future<String> getUser() async {
firestoreInstance.collection("User Data").get().then((querysnapshot) {
querysnapshot.docs.forEach((result) {
firestoreInstance
.collection("User Data")
.doc(result.id)
.collection("bank")
.where('account_username', isEqualTo: ownerData?.name2)
.get()
.then((query Snapshot) {
querysnapshot.docs.forEach((result) {
return result["bank_name"];
});
});
});
});
}
The Futurebuilder should look like this
FutureBuilder<String>(
future: getUser(), // async work
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting: return Text('Loading....');
default:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else
return Text('Result: ${snapshot.data}');
}
I want to search through my firebase collection and get any document with the specific phonenumber entered, but it keeps returning nothing.
Here is a screensot of my database
Here is the code i used to query it:
await FirebaseFirestore.instance
.collection("users")
.where("mobilenumber", isEqualTo: "+23407063435023")
.get()
.then((QuerySnapshot querySnapshot) { querySnapshot.docs.map((DocumentSnapShot documentSnapshot) {
if (documentSnapshot.exists) {
print("found");
}
});
}).catchError((error) => throw error);
and this is the security rule
The problem was in then, I should have used foreach instead of a map,
await FirebaseFirestore.instance
.collection("users")
.where("mobilenumber",isEqualTo: "+23409056088820")
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
print(documentSnapshot.id);
}
});
}).catchError((error) {
print(error);
});
I don't know why though.
Your query searches docs on country, not on phonenumber. Adapt it as follows:
await FirebaseFirestore.instance
.collection("users")
.where("phonenumber", isEqualTo: "12345")
.get()
.then((QuerySnapshot querySnapshot) { querySnapshot.docs.map((DocumentSnapShot documentSnapshot) {
if (documentSnapshot.exists) {
print("found");
}
});
}).catchError((error) => throw error);
Im trying to check if a document in my firebase console exist but I cannot use doc and dont know why . Maybe anyone can help heres my code
#override
void initState() {
super.initState();
getalldata();
}
getalldata() async {
//get videos as future
myVideos = FirebaseFirestore.instance
.collection('videos')
.where('uid', isEqualTo: widget.userid)
.get();
var documents = await FirebaseFirestore.instance
.collection('videos')
.where('uid', isEqualTo: widget.userid)
.get();
for (var item in documents.docs) {
likes = item.data()['likes'].length + likes;
}
setState(() {
dataisthere = true;
});
}
So in the documents var I wanna check if documents exists.
And them I wanna use the value inside an if else check
#override
void initState() {
super.initState();
getalldata();
}
getalldata() async {
//get videos as future
myVideos = await FirebaseFirestore.instance
.collection('videos')
.where('uid', isEqualTo: widget.userid)
.get();
var documents = await FirebaseFirestore.instance
.collection('videos')
.where('uid', isEqualTo: widget.userid)
.get();
if(documents.docs.isNotEmpty){
for (var item in documents.docs) {
likes = item.data()['likes'].length + likes;
}
setState(() {
dataisthere = true;
});
} else{
setState(() {
dataisthere = false;
});
}
}
I want to check whether the document exist or not without creating the document if it does not exits
Checked() {
Future<DocumentSnapshot> check = linkref.
document(user.UID).
collection("Requests").
document(uuid).get();
return FutureBuilder(
future: check,
builder: (context, ccheck) {
if (check != null ) {
return Text("Available");
}
return Text("not available);
});
}
i tried this code but even if the document does not exists it says that it exists
You should use; if (ccheck.data.exists) instead of if (check != null ). Here is the code;
Checked() {
Future<DocumentSnapshot> check =
linkref.document(user.UID).collection("Requests").document(uuid).get();
return FutureBuilder<DocumentSnapshot>(
future: check,
builder: (context, ccheck) {
if (ccheck.data.exists) {
return Text("Available");
}
return Text("not available");
});
}
You can use the .where( field, isEqualTo: query). This might be useful to you.
final userRef = FirebaseFirestore.instance.collection('users');
checkExists(String query) async {
QuerySnapshot checker = await userRef
.where('uid', isEqualTo: query)
.get();
chkr.docs.forEach((doc) {
if (doc.exists) {
print('Exists');
print(doc.get('uid'));
}else {
print('false');
}
});
}
Then, if you are using a button, you can use onPressed: () => check(yourQuery).
I would like to ask whats going on with my code.
Assuming the 'Counter' field is 179 in this instance, how do I make my outside myData update before printing?
class Test {
Firestore _firestore = Firestore.instance;
var myData;
void getData() async {
DocumentSnapshot snapshot =
await _firestore.collection('Counter').document('Counter').get();
myData = await snapshot.data['Counter'];
print('inside $myData');
}
void checkMyData() {
myData = 5;
getData();
print('outside $myData');
}
}
Console:
flutter: outside 5
flutter: inside 179
You have to make getData() return a Future like this:
Future getData() async {
So you can do this:
getData().then((value) {
print('value: $value');
}).catchError((error) {
print('error: $error');
});
But you probably want to use a FutureBuilder to show the information when arrives, like this:
FutureBuilder(
future: getData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text('value: ${snapshot.data}');
} else if (snapshot.hasError){
return Text('error: ${snapshot.error}');
}
return Text('loading...');
},
)