skip to Main Content

I faced a problem while try building an application.
the problem is that trying to set the field in SomeClass with a general setField function.
my implementation was like this but faced an issue withthis[fieldName];


EDITED

class TestClass {
  String name; // <- to set this the memberName = 'name';
  int age; // <- to set this the memberName = 'age';
  // and both will use the same setField as setter.

  TestClass({required name, required age});
  // the prev code is correct and no problem with it. 


  /** the use will be like this to set the value of name **/
  /** test.setField(memberName : 'name', valueOfThatMemberName: 'test name'); // notice here **/

  /** the use will be like this to set the value of age **/
  /** test.setField(memberName : 'age', valueOfThatMemberName: 15); // notice here **/

  void setField({required String memberName, required var valueOfThatMemberName}) {
    // some extra validation and logic,..
    this[memberName] = valueOfThatMemberName; // this gives this error:
  /** Error: The operator '[]=' isn't defined for the class 'TestClass'. **/
  }
  
  // this will return the valueOfThePassedMemberName;
  getField({required String memberName}) {
    return this[memberName]; // <= this gives this error
  /** Error: The getter 'memberName' isn't defined for the class 'TestClass'. **/
    
  }
}


void main() {
  TestClass test = TestClass(name: 'alaa', age: 14);

  /** here is the way to use it. **/
  test.setField(memberName: 'name', valueOfThePassedMemberName: 'test name'); // notice here
  test.setField(memberName: 'age', valueOfThePassedMemberName: 16); // notice here
  print(test.getField(memberName: 'name')); // <- this should print the name of test object.
}

setting the values just through the setField method.


ADDING RUNABLE JS CODE

// i need to do the exact same thing here with the dart.
export class Entity {
  constructor(data: {}) {
    Object.keys(data).forEach(key => {
      this.set(key, data[key], true);
    });
  }

  get(field: string) {
    return this["_" + field];
  }

  set(field: string, value: any, initial = false) {
    this["_" + field] = value;
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    After a lot of researching, I found that there is no way to do it directly, but there is another way i'm gonna drive you into the steps:

    1- Install the package

    we need to install Reflectable

    Readme Changelog Example Installing Versions Scores Use this package as a library Depend on it Run this command:

    With Dart:

    dart pub add reflectable
    

    With Flutter:

    flutter pub add reflectable
    

    2- Import the package into the class

    import 'package:reflectable/reflectable.dart';
    

    3- Create the Reflectable Class

    class MyReflectable extends Reflectable {
        const MyReflectable() : super(invokingCapability);
    }
    
    const myReflectable = MyReflectable();
    

    4- Add the Annotation into the class

    @myReflectable
    class TestClass {
       late String name;
       late int age;
    
       setField({required String fieldName, value}) {
           var instanceMirror = myReflectable.reflect(this);
           instanceMirror.invokeSetter(fieldName, value);
       }
    
       getField({required String fieldName}) {
           var instanceMirror = myReflectable.reflect(this);
           return instanceMirror.invokeGetter(fieldName);
       }
    }
    

    6- build main.reflectable.dart

    run this line in project root folder

    dart run build_runner build
    

    7- Initialize the Refactable

    import this line into the main.dart

    import 'package:erp/main.reflectable.dart';
    

    add this line into your main.

    initializeReflectable();
    

    8- Use it

    TestClass test = TestClass();
    test.setField(fieldName: 'name', value: 'Alaa');
    test.getField(fieldName: 'name'); // <- this will return 'Alaa'
    test.setField(fieldName: 'name', value: 'Ahmad');
    test.getField(fieldName: 'name'); // <- this will return 'Ahmad'
    
    // and the same for any other field.
    
    test.setField(fieldName: 'age', value: 14);
    test.getField(fieldName: 'age'); // <- this will return 14
    test.setField(fieldName: 'age', value: 20);
    test.getField(fieldName: 'age'); // <- this will return 20
    

    Finally Thanks to who contribute to try to answer and help :-)


  2. class TestClass {
      late String fieldName;
      late dynamic value;
    
      TestClass({required fieldName, required value});
    
      void setField({required String fieldName, required var value}) {
        // some extra validation and logic,..
        this.fieldName = fieldName;
        this.value = value;
      }
    
      getField() {
        return fieldName;
      }
      
      getValue() {
        return value;
      }
    }
    
    
    void main() {
      TestClass test = TestClass(fieldName: 'name', value: 'Alaa');
      
      
      test.setField(fieldName: 'name', value: 'Alaa');
      print('${test.getField()}: ${test.getValue()} ');
      
      test.setField(fieldName: 'age', value: 14);
      print('${test.getField()}: ${test.getValue()} ');
      
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search