@@ -415,7 +415,22 @@ class BoolSetting extends Setting<bool> {
415415 return defaultValue.toString ();
416416 }
417417
418+ /// Creates a boolean setting from a map representation.
419+ factory BoolSetting .fromMap (Map <String , dynamic > map) {
420+ return BoolSetting (
421+ key: map['key' ] as String ,
422+ defaultValue: map['defaultValue' ] as bool ,
423+ userConfigurable: map['userConfigurable' ] as bool ? ?? true ,
424+ validator: null , // Todo: implement validator deserialization
425+ onValidationError: null , // Handlers cannot be serialized
426+ );
427+ }
428+
418429 /// Creates a boolean setting from a JSON string representation.
430+ factory BoolSetting .fromJson (String json) {
431+ final map = jsonDecode (json) as Map <String , dynamic >;
432+ return BoolSetting .fromMap (map);
433+ }
419434}
420435
421436/// A setting that stores integer numeric values.
@@ -452,6 +467,23 @@ class IntSetting extends Setting<int> {
452467 super .validator,
453468 super .onValidationError,
454469 }) : super (type: SettingType .int );
470+
471+ /// Creates an integer setting from a map representation.
472+ factory IntSetting .fromMap (Map <String , dynamic > map) {
473+ return IntSetting (
474+ key: map['key' ] as String ,
475+ defaultValue: map['defaultValue' ] as int ,
476+ userConfigurable: map['userConfigurable' ] as bool ? ?? true ,
477+ validator: null , // Todo: implement validator deserialization
478+ onValidationError: null , // Handlers cannot be serialized
479+ );
480+ }
481+
482+ /// Creates an integer setting from a JSON string representation.
483+ factory IntSetting .fromJson (String json) {
484+ final map = jsonDecode (json) as Map <String , dynamic >;
485+ return IntSetting .fromMap (map);
486+ }
455487}
456488
457489/// A setting that stores double-precision floating-point values.
@@ -488,6 +520,23 @@ class DoubleSetting extends Setting<double> {
488520 super .validator,
489521 super .onValidationError,
490522 }) : super (type: SettingType .double );
523+
524+ /// Creates a double setting from a map representation.
525+ factory DoubleSetting .fromMap (Map <String , dynamic > map) {
526+ return DoubleSetting (
527+ key: map['key' ] as String ,
528+ defaultValue: map['defaultValue' ] as double ,
529+ userConfigurable: map['userConfigurable' ] as bool ? ?? true ,
530+ validator: null , // Todo: implement validator deserialization
531+ onValidationError: null , // Handlers cannot be serialized
532+ );
533+ }
534+
535+ /// Creates a double setting from a JSON string representation.
536+ factory DoubleSetting .fromJson (String json) {
537+ final map = jsonDecode (json) as Map <String , dynamic >;
538+ return DoubleSetting .fromMap (map);
539+ }
491540}
492541
493542/// A setting that stores string text values.
@@ -524,6 +573,23 @@ class StringSetting extends Setting<String> {
524573 super .validator,
525574 super .onValidationError,
526575 }) : super (type: SettingType .string);
576+
577+ /// Creates a string setting from a map representation.
578+ factory StringSetting .fromMap (Map <String , dynamic > map) {
579+ return StringSetting (
580+ key: map['key' ] as String ,
581+ defaultValue: map['defaultValue' ] as String ,
582+ userConfigurable: map['userConfigurable' ] as bool ? ?? true ,
583+ validator: null , // Todo: implement validator deserialization
584+ onValidationError: null , // Handlers cannot be serialized
585+ );
586+ }
587+
588+ /// Creates a string setting from a JSON string representation.
589+ factory StringSetting .fromJson (String json) {
590+ final map = jsonDecode (json) as Map <String , dynamic >;
591+ return StringSetting .fromMap (map);
592+ }
527593}
528594
529595/// A setting that stores lists of string values.
@@ -560,6 +626,23 @@ class StringListSetting extends Setting<List<String>> {
560626 super .validator,
561627 super .onValidationError,
562628 }) : super (type: SettingType .stringList);
629+
630+ /// Creates a string list setting from a map representation.
631+ factory StringListSetting .fromMap (Map <String , dynamic > map) {
632+ return StringListSetting (
633+ key: map['key' ] as String ,
634+ defaultValue: List <String >.from (map['defaultValue' ] as List ),
635+ userConfigurable: map['userConfigurable' ] as bool ? ?? true ,
636+ validator: null , // Todo: implement validator deserialization
637+ onValidationError: null , // Handlers cannot be serialized
638+ );
639+ }
640+
641+ /// Creates a string list setting from a JSON string representation.
642+ factory StringListSetting .fromJson (String json) {
643+ final map = jsonDecode (json) as Map <String , dynamic >;
644+ return StringListSetting .fromMap (map);
645+ }
563646}
564647
565648/// Represents the result of a validation operation.
@@ -591,3 +674,27 @@ class ValidationResult<T> {
591674 : 'ValidationResult(invalid: $value , error: $errorDescription )' ;
592675 }
593676}
677+
678+ class SettingFactory {
679+ /// Creates a Setting instance from a map or JSON representation.
680+ static Setting fromMap (Map <String , dynamic > map) {
681+ final typeString = map['type' ] as String ;
682+ final type = SettingType .values.firstWhere (
683+ (e) => e.name == typeString,
684+ orElse: () => throw ArgumentError ('Invalid setting type: $typeString ' ),
685+ );
686+
687+ switch (type) {
688+ case SettingType .bool :
689+ return BoolSetting .fromMap (map);
690+ case SettingType .int :
691+ return IntSetting .fromMap (map);
692+ case SettingType .double :
693+ return DoubleSetting .fromMap (map);
694+ case SettingType .string:
695+ return StringSetting .fromMap (map);
696+ case SettingType .stringList:
697+ return StringListSetting .fromMap (map);
698+ }
699+ }
700+ }
0 commit comments