buildContextFields method
List<ContextField>
buildContextFields(
- Map<
String, dynamic> fields, - List<
NestedContextField> nestedFields
Converts a { fieldName: schemaType } map to a list of ContextFields.
Nested objects and lists of objects are lifted into NestedContextField entries so templates can generate custom model classes.
Implementation
List<ContextField> buildContextFields(
Map<String, dynamic> fields,
List<NestedContextField> nestedFields,
) {
return fields.entries.map((entry) {
final value = entry.value;
final originalKey = entry.key;
final camelCaseKey = originalKey.contains('_') ? originalKey.toCamelCase() : originalKey;
final hasJsonKey = camelCaseKey != originalKey;
// If nested JSON object → treat as custom model type
if (value is Map<String, dynamic>) {
final someData = buildContextFields(value, nestedFields);
nestedFields.add(
NestedContextField(name: camelCaseKey.camelCaseToPascalCase(), properties: someData),
);
return ContextField(
name: camelCaseKey,
type: camelCaseKey.camelCaseToPascalCase(),
isCustom: true,
jsonKey: originalKey,
hasJsonKey: hasJsonKey,
);
}
// If list of objects → List<CustomModel>
if (value is List && value.isNotEmpty) {
var type = "";
var isCustom = false;
if (value.first is Map<String, dynamic>) {
nestedFields.add(
NestedContextField(
name: camelCaseKey.camelCaseToPascalCase(),
properties: buildContextFields(value.first, nestedFields),
),
);
type = camelCaseKey.camelCaseToPascalCase();
isCustom = true;
} else {
type = getDartType(value.first);
}
return ContextField(
name: camelCaseKey,
type: "List<$type>",
isList: true,
isCustom: isCustom,
jsonKey: originalKey,
hasJsonKey: hasJsonKey,
);
}
final type = getDartType(value);
return ContextField(
name: camelCaseKey,
type: type,
isList: type.contains('List'),
jsonKey: originalKey,
hasJsonKey: hasJsonKey,
);
}).toList();
}