Skip to content

Instantly share code, notes, and snippets.

@yeasin50
Last active September 2, 2021 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeasin50/eef18f25f6d9a489e70c5a8892fbfdce to your computer and use it in GitHub Desktop.
Save yeasin50/eef18f25f6d9a489e70c5a8892fbfdce to your computer and use it in GitHub Desktop.
void main() {
final List<Map<String, Object>> perguntas = [
{
'texto': 'Qual é a sua cor favorita?',
'respostas': ['Preto', 'Vermelho', 'Verde', 'Branco'],
},
{
'texto': 'Qual é o seu animal favorito?',
'respostas': ['Coelho', 'Cobra', 'Elefante', 'Leão'],
},
{
'texto': 'Qual é o seu instrutor favorito?',
'respostas': ['Jacob', 'Rodrigo', 'Daniel', 'Leo'],
},
];
int? foundValueAt;
print(perguntas.length);
for (int i = 0; i < perguntas.length; i++) {
final items = perguntas[i]['respostas'] as List<String>;
// finding where `Elefante` contains
print(items.contains("Elefante") ? "YEs" : "NO");
if (foundValueAt == null && items.contains("Elefante")) {
foundValueAt = i;
///finding index of `Elefante` inside of list<String>
final index = items.indexWhere((element) => element == 'Elefante');
print("index of Elefante => $index");
///let's change the value
perguntas[i]['respostas'] = items
..removeAt(index)
..add("newValue");
}
}
print('Elefante found onMainList: index $foundValueAt');
print("new Value>> ");
perguntas.forEach((e) {
print(e.toString());
});
}
@yeasin50
Copy link
Author

yeasin50 commented Sep 2, 2021

Result

3
NO
YEs
index of Elefante => 2
NO
Elefante found onMainList:  index 1
new Value>> 
{texto: Qual é a sua cor favorita?, respostas: [Preto, Vermelho, Verde, Branco]}
{texto: Qual é o seu animal favorito?, respostas: [Coelho, Cobra, Leão, newValue]}
{texto: Qual é o seu instrutor favorito?, respostas: [Jacob, Rodrigo, Daniel, Leo]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment