Access properties of a flutter class through the content of a variable

1.7k Views Asked by At

I'm in Flutter (Dart), I want to access to a property of a class, but the property that I have to access is defined by the content of another variable.

Example: class

   class Fruit{
     String get apple {return 'The apples are red';}
     String get orange {return 'The orange are orange';}
   }

And I have a variable String variableName = 'apple'

I want to do

Fruit f = Fruit();
f.(variableName)

I don't know if is possible... Thanks in advance

Edited

Perhaps the example was very bad. I'm working with flutter_intl and the vscode extension, it automatically generates a class, AppLocations in my case like this:


class AppLocalizations {
  AppLocalizations();
...

  /// `Server failure`
  String get failure_server_failure {
    return Intl.message(
      'Server failure',
      name: 'failure_server_failure',
      desc: '',
      args: [],
    );
  }

  /// `Email already in use`
  String get failure_email_already_in_use {
    return Intl.message(
      'Email already in use',
      name: 'failure_email_already_in_use',
      desc: '',
      args: [],
    );
  }
...
}

I have the translations in the ARB files in JSON, but I wanted to show an error message in the corresponding language.

{
  "failure_server_failure": "Server failure",
  "failure_email_already_in_use": "Email already in use"
}

And I want to do this Text( AppLocations.of(context).*here* ) And in here put the failure code.

2

There are 2 best solutions below

5
On

What if you define the class to accept a String. This will keep it generic enough to create multiple factory constructors

void main() {
  late Fruit a;
  late Fruit o;

  a = Fruit.apple();
  o = Fruit.orange();
  
  print(a);
  print(o);
}

class Fruit {
  final String text;

  const Fruit({required this.text});

  factory Fruit.empty() => Fruit(text: '');

  factory Fruit.apple() => Fruit(text: 'The apples are red');

  factory Fruit.orange() => Fruit(text: 'The oranges are orange');

  @override
  String toString() => 'The text is:  $text';
}

.......

0
On

This can only be accomplished using the dart:mirrors package which is not supported by Flutter.

However, after giving it a bit of thought, you could accomplish this by implementing a toJson method and a fromJson factory function in the class which properties you'd like to access by a variable name.

When you've converted the instance to json, which is of type Map<String, dynamic>, you can dynamically access and change any property of it, and then convert it back to the original class.

Like this:

class A {
  String name;
  int? age;

  A({required this.name, this.age});

  factory A.fromJson(Map<String, dynamic> json) => A(
    name: json['name'] as String,
    age: json['age'] as int?,
  );

  Map<String, dynamic> toJson() => {
    'name': name,
    if (age != null) 'age': age,
  };
}

void main() {
  var a = A(name: 'John');

  final json = a.toJson();

  var myVariable = 'name';

  json[myVariable] = 'Mary';
  
  json['age'] = 20;

  a = A.fromJson(json);

  print(a.name); // Mary
  print(a.age); // 20
}