Model constructor

Model({
  1. required String name,
  2. required String icon,
  3. required List<List<Field>> fields,
  4. bool isCollection = true,
  5. int sort = 0,
  6. bool showInMenu = true,
  7. String? id,
  8. bool codeFirstEntity = true,
  9. bool isHybrid = false,
})

Implementation

Model({
  required this.name,
  required this.icon,
  required this.fields,
  this.isCollection = true,
  this.sort = 0,
  this.showInMenu = true,
  String? id,
  this.codeFirstEntity = true,
  this.isHybrid = false,
}) : id = id ?? toSnakeCase(name) {
  /// ? FLATTEN FIELDS
  flattenFields = fields.fold(<Field>[], (List<Field> allFields, List<Field> currentRowFields) {
    for (final Field field in currentRowFields) {
      allFields.add(field);
      if (field.virtualFields.isNotEmpty) {
        allFields.addAll(field.virtualFields);
      }
    }
    return allFields;
  });

  /// ? LIST FIELDS
  final List<Field> listFieldsTemp = flattenFields.where((Field field) => field.showInList).toList();
  listFieldsTemp.sort((first, second) => first.sort.compareTo(second.sort));
  listFields = listFieldsTemp;

  /// ? ID FIELD
  final List<IdField> idFieldsTemp = flattenFields.whereType<IdField>().toList();
  assert(idFieldsTemp.isNotEmpty, 'You should specify at least a one $IdField at your model $runtimeType');
  idField = idFieldsTemp.first;

  /// ? DYNAMIC FIELDS IDS
  final List<String> dynamicFieldsTemp = [];
  for (final Field field in flattenFields) {
    if (field is DynamicField && field is! StructuredField && field is! StructureField) {
      dynamicFieldsTemp.add(field.id);
    }
  }
  dynamicFields = dynamicFieldsTemp;
}