coderForField method
String?
coderForField(
- FieldElement field,
- SharedChecker<
Model> checker, { - required bool wrappedInFuture,
- required Sqlite fieldAnnotation,
Produces serializing or deserializing method given a field and checker.
The assignment (data['my_field']:
in serializers or myField:
in deserializers)
is automatically injected by the superclass and should not be included in the
output of the coder.
To simplify checking, Future
s are unwrapped before they get to this method.
If the type was originally a future, wrappedInFuture
is true
.
For example, Future<List<int>>
will be an iterable according to the checker and
wrappedInFuture
will be true
.
Implementation
@override
String? coderForField(field, checker, {required wrappedInFuture, required fieldAnnotation}) {
final fieldValue = serdesValueForField(field, fieldAnnotation.name!, checker: checker);
// Iterable
if (checker.isIterable) {
final argType = checker.unFuturedArgType;
final argTypeChecker = OfflineFirstChecker(checker.argType);
final castIterable = SerdesGenerator.iterableCast(
argType,
isSet: checker.isSet,
isList: checker.isList,
isFuture: wrappedInFuture || checker.isFuture,
forceCast: true,
);
// Iterable<OfflineFirstSerdes>
if (argTypeChecker.hasSerdes) {
final doesHaveConstructor = hasConstructor(checker.argType);
if (doesHaveConstructor) {
final serializableType =
argTypeChecker.superClassTypeArgs.last.getDisplayString(withNullability: true);
return '''
jsonDecode($fieldValue).map(
(c) => $argType.$constructorName(c as $serializableType)
)$castIterable
''';
}
}
}
// OfflineFirstSerdes
if ((checker as OfflineFirstChecker).hasSerdes) {
final doesHaveConstructor = hasConstructor(field.type);
if (doesHaveConstructor) {
final serializableType =
checker.superClassTypeArgs.last.getDisplayString(withNullability: true);
return '${SharedChecker.withoutNullability(field.type)}.$constructorName($fieldValue as $serializableType)';
}
}
return super.coderForField(field, checker,
wrappedInFuture: wrappedInFuture, fieldAnnotation: fieldAnnotation);
}