ZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZ

Reputation: 45

How do i loop within the array field in firestore field and retrieve the last result that has true value to it?

""

Refer this image the module 0 has is locked equals to false and module 1 has isLocked equal to true so I should receive the name "What is spirituality".

I want to retrieve the name of next module who previous module has isLocked equal to false.

Upvotes: 1

Views: 58

Answers (1)

Morez
Morez

Reputation: 2229

You can get the full list and check your conditions.

DocumentSnapshot docSnapshot = Firestore.instance.collection("collectionName").document("docId").get(); //gets the data of your document => replace the collectionName and docId

bool previousIsLockedValue = false; //this keeps track of the previous module isLoked property value and the initial value is false so if the first module's isLocked is true then name will be retrieved
String nameOfModule;

List<dynamic> modules = docSnapshot.data["modules"] //gets the modules list from the document data

for (module in modules){
    if(module["isLocked"] == true && previousIsLockedValue == false){
       nameOfModule = module["name"]; 
       //assigns the module name if the previous isLocked value is false and this 
       //module isLocked value is true

       //Do something with the name ...
    }
    previousIsLockedValue = module["isLocked"];
}


You can change the initial value of previousIsLockedValue to true so you don't get the first module name if its IsLocked property is true

Upvotes: 1

Related Questions