Reputation:
I have a List Like this:
final List _subcoll=[
{'name':'Java','pdfurl':'pdf1','thumbnail':'1'},
{'name':'Dart','pdfurl':'pd2','thumbnail':'2'},
{'name':'JavaScript','pdfurl':'pdf3','thumbnail':'3'},
{'name':'PHP','pdfurl':'pdf4','thumbnail':'4'},
{'name':'Python','pdfurl':'pdf5','thumbnail':'5'},
];
How Can I Access a Single value such as name= java, and pdffile=pdf1?`
Upvotes: 0
Views: 147
Reputation: 1385
If you want to access an element of a list by index just use square brackets [
]
:
final e = _subcoll[idx];
Dart will guess the type (at compile-time) here, for non-final
variables you can use var
.
In your case, an element is probably of type Map<String, String>
, you can be explicit and specify it:
final Map<String, String> e = _subcoll[idx];
You can get the index of the first matching item with:
final int i = _subcoll.indexWhere((e)=>e["name"]=="java" && e["pdffile"]="pdf1");
The first matching item with:
final e = _subcoll.firstWhere((e)=>e["name"]=="java" && e["pdffile"]="pdf1");
Or an Iterable with all matching items with:
for(final e in _subcoll.where((e)=>e["name"]=="java" && e["pdffile"]="pdf1"){
doSmth();
}
Upvotes: 1
Reputation: 8978
I don't know whether you have found an answer to that, but here is the solution [more basic precisely] which you want to do in order to get the desired result.
Basically, it is just looping through the array, and then fetching the data like this: array[index][dictionary_key_name]
Dictionary_key is nothing but {} data
// In this case we have keys like 'name', 'pdfurl' and value like 'Java', and 'thumbnail'
// Every key is connected to the value in dictionary or hashmap/hashmap
dictionary = {key: value}
void main(){
final List _subcoll=[
{'name':'Java','pdfurl':'pdf1','thumbnail':'1'},
{'name':'Dart','pdfurl':'pd2','thumbnail':'2'},
{'name':'JavaScript','pdfurl':'pdf3','thumbnail':'3'},
{'name':'PHP','pdfurl':'pdf4','thumbnail':'4'},
{'name':'Python','pdfurl':'pdf5','thumbnail':'5'},
];
/*
You can do whatever in the output you want
My output is to just show you how it is working
Just focus on how I got the result, _subscoll[index][hashkey]
*/
for(int i=0; i<_subcoll.length; i++){
print('name => ${_subcoll[i]['name']}, pdfurl => ${_subcoll[i]['pdfurl']}');
}
}
OUTPUT
name => Java, pdfurl => pdf1
name => Dart, pdfurl => pd2
name => JavaScript, pdfurl => pdf3
name => PHP, pdfurl => pdf4
name => Python, pdfurl => pdf5
Play around with that more, and let me know if you have any doubts with the result.
Upvotes: 0