6

I have stored variables in a class with their code names.

Suppose I want to get XVG from that class, I want to do

String getIconsURL(String symbol) {

  var list = new URLsList();

  //symbol = 'XVG'

  return list.(symbol);
}

class URLsList{

var XVG = 'some url';
var BTC = 'some url';

}

Can someone help me achieve this or provide me with a better solution?

4
  • return list.XVG ? Commented Apr 17, 2018 at 6:25
  • I want it to be dynamic, this is just an example, in real there are 1500 of these variable. I can't add that many switch statements. Commented Apr 17, 2018 at 6:27
  • Alright. I understood the question wrong. Nevertheless Zeeshan has answered the question already. Commented Apr 17, 2018 at 6:37
  • Zeeshan's answer was for Java. I am in Dart Flutter. Commented Apr 17, 2018 at 6:38

3 Answers 3

7

Dart when used in flutter doesn't support reflection.

If it's text that you want to have directly in your code for some reason, I'd advise using a text replace (using your favourite tool or using intellij's find + replace with regex) to change it into a map, i.e.

final Map<String, String> whee = {
  'XVG': 'url 1',
  'BTC': 'url 2',
};

Another alternative is saving it as a JSON file in your assets, and then loading it and reading it when the app opens, or even downloading it from a server on first run / when needed (in case the URLs need updating more often than you plan on updating the app). Hardcoding a bunch of data like that isn't necessarily always a good idea.

EDIT: how to use.

final Map<String, String> whee = .....

String getIconsURL(String symbol) {
  //symbol = 'XVG'

  return whee[symbol];
}

If you define it in a class make sure you set it to static as well so it doesn't make another each time the class is instantiated.

Also, if you want to iterate through them you have the option of using entries, keys, or values - see the Map Class documentation


Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I tried this it seems okay but how do I retrieve the value I can't seem to find anything related to that.
Dam this is kinda crappy for String langauge consistancy and autocompletion :-/
4

I'd just implement a getProperty(String name) method or the [] operator like:

class URLsList{
  var XVG = 'some url';
  var BTC = 'some url';

  String get operator [](String key) {
    switch(key) {
      case 'XVG': return XVG;
      case 'BTC': return BTC;
    }
  }
}

String getIconsURL(String symbol) {
  var list = new URLsList();
  return list[symbol];
}

You can also use reflectable package that enables you to use reflection-like code by code generation.

Comments

0

Assuming that the class is being created from a JSON Object, you can always use objectName.toJSON() and then use the variable names are array indices to do your computations.

Comments

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.