0

Hello I try to detect if I have same value between two keys in a String

I have a format like this

[
{array: id: idX, vote: X, description: textX}, 
{array: id: idX, vote: X, description: textX}
]

I would found a solution to determine the case there are best vote and case there are equal vote


case 1 there is a winner:

[
{array: id: id1, vote: 2, description: text1}, 
{array: id: id2, vote: 1, description: text2}
]

id1 = win => display text1


case 2 there is no winner:

[
{array: id: id1, vote: 2, description: text1}, 
{array: id: id2, vote: 2, description: text2}
]

id 1 & 2 = equal => wait message

0

1 Answer 1

1

What is the data type of your input? You mentioned it as String but I can see it is list of maps.

// your data
List<Map<String, dynamic>> data = [];

// here assuming vote will be always greater than 0
// therefore setting maxVote = -1
int maxVote = -1;
String displayText = "";

// starting from second element
for(int i=1; i< data.length; i++){
  if(data[i]['vote'] > data[i-1]['vote']){
    maxVote = data[i]['vote'];
    displayText = data[i]['description'];
  }
  if(data[i]['vote'] < data[i-1]['vote']){
    maxVote = data[i-1]['vote'];
    displayText = data[i-1]['description'];
  }
}

if(maxVote == -1){
  // all votes where equal
  print("wait message");
}else{
  print(displayText);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you I will try, it's a exactly final allData = querySnapshot.docs.map((doc) => doc.data()).toList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.