@@ -374,36 +374,36 @@ class CodeChunks {
374374 return readField ();
375375 }).toList (growable: false );
376376
377- // add initializers for relations
378- entity.properties.forEachIndexed ((int index, ModelProperty p) {
379- if (p.isRelation) {
380- postLines.add (
381- 'object.${propertyFieldName (p )}.targetId = ${fieldReaders [index ]};'
382- '\n object.${propertyFieldName (p )}.attach(store);' );
383- }
384- });
385-
386- postLines.addAll (entity.relations.map ((ModelRelation rel) =>
387- 'InternalToManyAccess.setRelInfo(object.${rel .name }, store, ${relInfo (entity , rel )}, store.box<${entity .name }>());' ));
388-
389- postLines.addAll (entity.backlinks.map ((ModelBacklink bl) {
390- return 'InternalToManyAccess.setRelInfo(object.${bl .name }, store, ${backlinkRelInfo (entity , bl )}, store.box<${entity .name }>());' ;
391- }));
392-
393377 // try to initialize as much as possible using the constructor
394378 entity.constructorParams.forEachWhile ((String declaration) {
395379 // See [EntityResolver.constructorParams()] for the format.
396- final paramName = declaration.split (' ' )[0 ];
397- final paramType = declaration.split (' ' )[1 ];
380+ final declarationParts = declaration.split (' ' );
381+ final paramName = declarationParts[0 ];
382+ final paramType = declarationParts[1 ];
383+ final paramDartType = declarationParts[2 ];
398384
399385 final index = fieldIndexes[paramName];
400- if (index == null ) {
386+ late String paramValueCode;
387+ if (index != null ) {
388+ paramValueCode = fieldReaders[index];
389+ if (entity.properties[index].isRelation) {
390+ if (paramDartType.startsWith ('ToOne<' )) {
391+ paramValueCode = 'ToOne(targetId: $paramValueCode )' ;
392+ } else if (paramType == 'optional-named' ) {
393+ log.info ('Skipping constructor parameter $paramName on '
394+ "'${entity .name }': the matching field is a relation but the type "
395+ "isn't - don't know how to initialize this parameter." );
396+ return true ;
397+ }
398+ }
399+ } else if (paramDartType.startsWith ('ToMany<' )) {
400+ paramValueCode = 'ToMany()' ;
401+ } else {
401402 // If we can't find a positional param, we can't use the constructor at all.
402- if (paramType == 'positional' ) {
403- log.warning ("Cannot use the default constructor of '${entity .name }': "
403+ if (paramType == 'positional' || paramType == 'required-named' ) {
404+ throw InvalidGenerationSourceError (
405+ "Cannot use the default constructor of '${entity .name }': "
404406 "don't know how to initialize param $paramName - no such property." );
405- constructorLines.clear ();
406- return false ;
407407 } else if (paramType == 'optional' ) {
408408 // OK, close the constructor, the rest will be initialized separately.
409409 return false ;
@@ -414,18 +414,20 @@ class CodeChunks {
414414 switch (paramType) {
415415 case 'positional' :
416416 case 'optional' :
417- constructorLines.add (fieldReaders[index] );
417+ constructorLines.add (paramValueCode );
418418 break ;
419- case 'named' :
420- constructorLines.add ('$paramName : ${fieldReaders [index ]}' );
419+ case 'required-named' :
420+ case 'optional-named' :
421+ constructorLines.add ('$paramName : $paramValueCode ' );
421422 break ;
422423 default :
423424 throw InvalidGenerationSourceError (
424425 'Invalid constructor parameter type - internal error' );
425426 }
426427
427- // Good, we don't need to set this field anymore
428- fieldReaders[index] = '' ; // don't remove - that would mess up indexes
428+ // Good, we don't need to set this field anymore.
429+ // Don't remove - that would mess up indexes.
430+ if (index != null ) fieldReaders[index] = '' ;
429431
430432 return true ;
431433 });
@@ -438,6 +440,23 @@ class CodeChunks {
438440 }
439441 });
440442
443+ // add initializers for relations
444+ entity.properties.forEachIndexed ((int index, ModelProperty p) {
445+ if (! p.isRelation) return ;
446+ if (fieldReaders[index].isNotEmpty) {
447+ postLines.add (
448+ 'object.${propertyFieldName (p )}.targetId = ${fieldReaders [index ]};' );
449+ }
450+ postLines.add ('object.${propertyFieldName (p )}.attach(store);' );
451+ });
452+
453+ postLines.addAll (entity.relations.map ((ModelRelation rel) =>
454+ 'InternalToManyAccess.setRelInfo(object.${rel .name }, store, ${relInfo (entity , rel )}, store.box<${entity .name }>());' ));
455+
456+ postLines.addAll (entity.backlinks.map ((ModelBacklink bl) {
457+ return 'InternalToManyAccess.setRelInfo(object.${bl .name }, store, ${backlinkRelInfo (entity , bl )}, store.box<${entity .name }>());' ;
458+ }));
459+
441460 return '''(Store store, ByteData fbData) {
442461 final buffer = fb.BufferContext(fbData);
443462 final rootOffset = buffer.derefObject(0);
0 commit comments