0

Objective

Create a method that sets value to the object created above.

This method could be used like below.

setValueToUser(userPropertyName, value);

Given

  • A mutable model made with freezed.
import 'package:freezed_annotation/freezed_annotation.dart';

part 'user.freezed.dart';
part 'user.g.dart';

@unfreezed
class User with _$User {
  factory User({
    required int id,
    required String email,
    required String password,
    required DateTime birthday,
  }) = _User;

  factory User.fromJson(Map<String, dynamic> json) =>
      _$UserFromJson(json);
}

What I tried

switch-case statement

First, I made something like this, but this ends up being very tedious as an actual user model has 109 property members :(

User setValueToUser(String propertyName, dynamic value){
   final user = User();
   switch (propertyName) {
      case 'id': 
         user.id = value as int;
         break
      case 'email': 
         user.email = value as String;
         break
      case 'password': 
         user.password = value as String;
         break
      case 'birthday': 
         user.birthday = value as DateTime;
         break
   }
   return user;
}

Map

Then, I created a map, but dart requires all fields of a map to be initialized when the map variable initializes, so failed achieve the goal.

User setValueToUser(String propertyName, dynamic value) {
   final user = User();
   final userDictionary =  <String, dynamic>{
      'id': user.id = value as int,
      'email': user.email = value as String,      
      'password': user.password = value as String,
      'birthday': user.birthday = value as DateTime,
   };
   userDictionary[propertyName];   // I intend to set value to ONLY one specific property at a time. Apparently, this line means nothing to dart :(
   return user;
}

Conclusion

I know setting a property name with string looks very ridiculous. I do want to avoid using string if possible, so comments and answers regarding this is very much welcomed. (But setting enum for all properties of the class would be tedious, I think)

HPanda
  • 23
  • 4
  • How about using a `freezed` union? – Ayan Sengupta Sep 09 '22 at 03:27
  • @AyanSengupta Thanks for an idea! I'm not familiar with unions, and does this mean to make as many constructors as a number of members in the user class? – HPanda Sep 09 '22 at 04:56
  • Never mind. Seems like that's not suitable for your use case. Unfortunately there is almost no support for runtime reflection/introspection in dart for the shake of "tree shaking". So the alternative method dart devs adopt is development time code generation using `build_runner` through which one can generate an extension method on an object that can return another object by mapping its properties from current object. Sadly enough, that's again a pretty vast topic to elaborate in a comment or answer. – Ayan Sengupta Sep 09 '22 at 05:37
  • @AyanSengupta hmm.. yeah I have seen a discussion on discontinued dart:mirror and is pretty disappointing. If there still is some ongoing discussion or an attempt on this topic that you know of, I would be pleased if you don’t mind sharing? – HPanda Sep 09 '22 at 07:45

0 Answers0