skip to Main Content

For example, every time I want to create a new entity or model, there is an attribute called global that I need to set to true or false depending on its other attribute called id :

If the id is 0, I want the global to be true

Entity :

class Folder extends Equatable {
  const Folder({
    required this.id,
    required this.global /// here i want this.global = (id == 0)
  });

  final int id;
  final bool global;

  @override
  List<Object> get props {
    return [
      id,
      global,
    ];
  }
}

Model :

class FolderModel extends Folder{
  FolderModel ({
    required this.id,
    required this.global,
  }) : super(
    id: id,
    global: global,
  );

  FolderModel copyWith({
    int? id,
    bool? global,
  }) {
    return FolderModel(
      id: id ?? this.id,
      global: global ?? this.global,
    );
  }

  Map<String, dynamic> toMap() {
    final result = <String, dynamic>{};
  
    result.addAll({'id': id});
    result.addAll({'global': global});
  
    return result;
  }

  factory FolderModel.fromMap(Map<String, dynamic> map) {
    return FolderModel(
      id: map['id']?.toInt() ?? 0,
      global: map['global'] ?? false,
    );
  }

  String toJson() => json.encode(toMap());

  factory FolderModel.fromJson(String source) => FolderModel.fromMap(json.decode(source));

}

Where should I add that, or should I create a special function to read that value ? or just don’t add anything and everything is logic out of these classes ?

3

Answers


  1. I think you can try use factory constructor :

    import 'package:equatable/equatable.dart';
    
    class Folder extends Equatable {
      const Folder({required this.id, required this.global});
    
      final int id;
      final bool global;
    
      factory Folder.byId(int id, {bool? global}) {
        return Folder(id: id, global: global ?? id == 0);
      }
    
      @override
      List<Object> get props {
        return [
          id,
          global,
        ];
      }
    }
    
    Login or Signup to reply.
  2. IMO this will work for your issue:

    class Folder extends Equatable {
      Folder({required this.id, required this.global
          }) {
        global = (id == 0);
      }
    
      int id;
      bool global;
    
      @override
      List<Object> get props {
        return [
          id,
          global,
        ];
      }
    }
    
    Login or Signup to reply.
  3. The best way would be to make global a getter, since you don’t use it anyway.

    In case you do want the explicit option of having a global with an id, the simplest way to achieve this is:

    class Folder {
      const Folder({
        required this.id,
        bool? global
      }) : global = (global ?? (id == 0));
    
      final int id;
      final bool global;
      
      @override
      String toString() {
          return 'id=$id, global: $global';
      }
    }
    
    
    void main() {
      const configurable = Folder(id: 17, global: true);
      const automaticGlobal = Folder(id: 0);
      const automaticNonGlobal = Folder(id: 17);
      
      print(configurable);
      print(automaticGlobal);
      print(automaticNonGlobal);
    }
    

    By making global a non-required named parameter, it is optional. If you set it, the value will be used, if you don’t, the value will be calculated.

    This will print:

    id=17, global: true
    id=0, global: true
    id=17, global: false
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search