Nitneuq
Nitneuq

Reputation: 5022

how to compare two values from a string of keys/values in flutter?

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

Upvotes: 0

Views: 654

Answers (1)

Kartik Patel
Kartik Patel

Reputation: 627

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);
}

Upvotes: 1

Related Questions