How to update nested field inside a document in Firestore Flutter - flutter

I am working with Flutter and Cloud Firestore, and I'm stuck on the point where I need to update a nested field inside my collection -> document. Below is the screenshot of my firestore collection structure. I know how to do basic update method like FirebaseFirestore.instance.collection('collection_path').doc('data').update(...), however, in my case, the document structure is a bit complex so I need some help in case of performing update method.
Here, I need to change (on a button press) the field status to true/false under strategies array.
I have the access to strategyName field so that I could get that item in the strategies array and change it...
What I tried:
ElevatedButton(
onPressed: () async {
// status=true
final value = await FirebaseFirestore.instance
.collection(widget.symbol + '_Live')
.doc("data")
.get();
final data = value.data();
final strategies =
data!['strategies'] as List<Map<String, dynamic>>;
final strategy = strategies.firstWhere(
(item) => item['strategyName'] == strategyName,
orElse: () => Map());
strategy['status'] = true;
await FirebaseFirestore.instance
.collection(widget.symbol + '_Live')
.doc("data")
.update(data);
},
child: Text("Start"),
),
Obviously, this won't work because here I'm just changing it locally, i.e. not using the Firestore update method to actually change the field. But I'm confused as to how to update that nested field.
Any leads/help would be really appreciated!
EDIT:
I did as you told and edited my answer, but now the error is gone but nothing seems to work, i.e. in firestore, there is no change in the data doc.
ERROR: Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast

You need to update value map locally, then store it back:
final value = await FirebaseFirestore.instance
.collection(symbol + '_Live')
.doc("data")
.get();
final data = value.data();
final strategies =
data!['strategies'].map((item) => item as Map<String, dynamic>).toList();
final strategy = strategies.firstWhere(
(item) => item['strategyName'] == strategyName,
orElse: () => Map());
strategy['status'] = toggleValue;
await FirebaseFirestore.instance
.collection(symbol + '_Live')
.doc("data")
.update(data);
The point is to store the entire value.
EDIT by Author : Just needed to map each element to Map<String, dynamic>.

Related

The operator '[]' isn't defined for the type 'Object'.Getting data from one collection and adding it to another

In my firestore, i have a creators collection created and i am trying to create an edit profile collection. i want to get the value of a field in the creators collection and add it to the edit profile collection. After some research, i found out a way to go about doing this but when i want to set the value to the field, i get the error above. I would really appreciate some help.
here is the code
editProfile()async{
CollectionReference creatorCollection = FirebaseFirestore.instance.collection('Creators');
CollectionReference editProfileCollection = FirebaseFirestore.instance.collection('EditProfile');
String profileImageUrl =await uploadProfilePictureToStorage(_image);
String profilePicUrl =await uploadProfilePictureToStorage(_profilePic);
creatorCollection.where("fullName", isEqualTo: widget.enterName).get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
final DocumentSnapshot creatorDoc = result;
editProfileCollection.doc(FirebaseAuth.instance.currentUser!.uid).set({
'FullName': creatorDoc.data()?["fullName"],
'EditedFullName': _name,
'location': _location,
'links': _linked,
'contact':_contact,
'dob':_dob,
'uid':FirebaseAuth.instance.currentUser!.uid,
'likes':[],
'Headers': profileImageUrl,
'ProfilePic':profilePicUrl
}).whenComplete((){
Navigator.pop(context);
});
});
});
what i am trying to get from the creators collection is the fullname. If there is a better way to go about getting the value, that would also be appreciated. Here is the collection by the way
You can use .get or use as Map? like 'FullName': (creatorDoc.data() as Map?)?["fullName"],
'FullName': creatorDoc.get("fullName"),

Why is vs code saying the method '.getDocuments' and the getter documents are not defined for types Query and QuerySnapshot respectively?

void _getQuestions() async {
// Query Firestore for questions with the specified tags
Query query = await _firestore
.collection('questions')
.where('tags', arrayContainsAny: widget.tags);
QuerySnapshot querySnapshot = await query.getDocuments();
setState(() {
_questions = querySnapshot.documents;
});
importing cloud_firestore.dart.
I expected the errors to leave, but they are still around.
The method to get the documents is called get() in Flutter, not getDocuments().
I recommend keeping the Firebase documentation handy for this sort of thing, for this case that'd be the section on getting multiple documents from a collection

Get list of collection from firebase using flutter

I need to get list of collection like
datalist = ["2009" , "2010"]
I can use this code to print what inside the 2009 , but that not i want
final _fireStore = FirebaseFirestore.instance;
Future<void> getData() async {
// Get docs from collection reference
QuerySnapshot querySnapshot =
await _fireStore.collection('data/student_details/2009').get();
// Get data from docs and convert map to List
final allData = querySnapshot.docs.map((doc) => doc.data()).toList();
//for a specific field
print(allData);
}
and I need to know about can I filter it
Thank you.
My Firebase view
In Firebase you can not fetch a list of Collections, You have to explicitly mention the name of Collection to fetch it. It means you must have pre-knowledge of name of Collection to fetch it.

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Map<dynamic, dynamic>>' Flutter Firebase

I am trying to get a list from firebase document and then set to another empty List of maps then add a map to this list of maps and pass it back to Firebase.
This is My method:
Future logFinalWorkout(user, finalWorkout) async{
List<Map<dynamic, dynamic>> result = List<Map<dynamic, dynamic>>();
await users.document(user.uid).get().then((doc){
result = doc.data['finalWorkouts'];
result.addAll(finalWorkout);
});
return await users.document(user.uid).updateData({
'finalWorkouts' : result,
});
}
finalWorkout parameter is of type Map<dynamic,dynamic>
final workouts represents the list coming from Firebase. I keep getting the above error
Remove List<Map<dynamic, dynamic>> result and add List< dynamic> result instead of that. Give it a try.

FLUTTER FIRESTORE -Query the field and get document id and check other field

am trying to get the searched value(userid) which is in the field of a document in firestore I want to check the other fields(status) of the document I tried this method but failed
handlesubmit(BuildContext context)async{
final QuerySnapshot searcheduserid=
await Firestore.instance.collection('users')
.where('userid',isEqualTo: userid).limit(1).getDocuments();
final userdocid=searcheduserid.documents.map((doc)=>doc.documentID);
final DocumentSnapshot getuserdoc= await Firestore.instance.collection('users')
.document(userdocid).get();
final userstatus = getuserdoc.data['status'];
// I GET AN ERROR HERE ERROR SAYS
// METHOD [](status) was called on null
if(userstatus==null){
return showdialog( context,'the entered user id status does not exist');
}
}
You probably copied an older version of your code because it is unlikely that your code compiles the following line of your program:
final DocumentSnapshot getuserdoc= await Firestore
.instance
.collection('users')
.document(userdocid).get();
The error message on my system is:
The argument type 'Iterable<String>' can't be assigned to the parameter type 'String'.
Which means that userdocid is an Iterable of type String (Iterable<String>), but a parameter which is of type String is expected.
You have multiple options of fixing the problem, but I suggest the following:
Since you are only expecting one document from your QuerySnapshot it is enough to just look at the first document.
final QuerySnapshot searchedUserId = await Firestore.instance
.collection('users')
.where('userid', isEqualTo: userid)
.limit(1)
.getDocuments();
// if it is possible that searchedUserId returns no document make sure to
// check whether searchedUserId.documents.length > 0,
// otherwise searchedUserId.documents.first will throw an error
DocumentSnapshot document = searchedUserId.documents.first;
final userDocId = document.documentID;
final DocumentSnapshot getuserdoc =
await Firestore.instance.collection('users').document(userDocId).get();
Improved Solution:
However I think your code is a bit redundant anyways, because you are looking up a field of a document which has the same value as the document ID. You could shorten the whole code to
final DocumentSnapshot getuserdoc =
await Firestore.instance.collection('users').document(userid).get();
Error calling getuserdoc.data['status']
If you get an error saying something similar to
[](status) was called on null
Then that means getuserdoc has no value. This is likely due to the fact that there is no such database entry with the given ID. Check if the ID is in your database, otherwise comment below for additional information, because the provided code had compilation errors and does not run at all.