Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a list from an object in dart

Tags:

dart

I come from as3 environment, so I am big beginner in dart and HTML.

I have this variable

var symbols = [
               {"name":"first",
                 "num":[2,2,3]
               }];

I want to access num array and save it to variable, I try to do this:

var symbol = symbols[0];
var num = symbol.num;

I get

Breaking on exception: Class '_LinkedHashMap' has no instance getter 'num'.

Can you help me please?

like image 633
Tree Avatar asked Nov 03 '14 18:11

Tree


2 Answers

What you have is a list of a map of String to String. You could write the type like this:

List<Map<String, String>> symbols;

so naturally what you want to access is the value for the key "num". You can do this:

symbols[0]['num']

but it doesn't automatically gets mapped to an instance variable by writing:

symbol.num

If you had a class like this, the above would work:

class Symbol {
    var num;
}
like image 163
QQQ Avatar answered Sep 28 '22 05:09

QQQ


It doesn't work this way in Dart.
Try:

symbols[0]['num']
like image 36
Günter Zöchbauer Avatar answered Sep 28 '22 05:09

Günter Zöchbauer