Extending auto-generated Amplify Datastore classes with factory constructors

0 votes

The base model generated through AWS Amplify Studio:

@immutable
class Patient extends Model {
  static const classType = const _PatientModelType();
  final String id;
  final String? _name;
  final String? _family_name;
  final TemporalDate? _dob;
  final Gender? _gender;
  final double? _height_in_inches;
  final List<Assessment>? _Assessments;
  final TemporalDateTime? _createdAt;
  final TemporalDateTime? _updatedAt;

 ...
  
  const Patient._internal({required this.id, required name, required family_name, required dob, required gender, required height_in_inches, Assessments, createdAt, updatedAt}): _name = name, _family_name = family_name, _dob = dob, _gender = gender, _height_in_inches = height_in_inches, _Assessments = Assessments, _createdAt = createdAt, _updatedAt = updatedAt;
  
  factory Patient({String? id, required String name, required String family_name, required TemporalDate dob, required Gender gender, required double height_in_inches, List<Assessment>? Assessments}) {
    return Patient._internal(
      id: id == null ? UUID.getUUID() : id,
      name: name,
      family_name: family_name,
      dob: dob,
      gender: gender,
      height_in_inches: height_in_inches,
      Assessments: Assessments != null ? List<Assessment>.unmodifiable(Assessments) : Assessments);
  }
  
  ...
  
  Patient.fromJson(Map<String, dynamic> json)  
    : id = json['id'],
      _name = json['name'],
      _family_name = json['family_name'],
      _dob = json['dob'] != null ? TemporalDate.fromString(json['dob']) : null,
      _gender = enumFromString<Gender>(json['gender'], Gender.values),
      _height_in_inches = (json['height_in_inches'] as num?)?.toDouble(),
      _Assessments = json['Assessments'] is List
        ? (json['Assessments'] as List)
          .where((e) => e?['serializedData'] != null)
          .map((e) => Assessment.fromJson(new Map<String, dynamic>.from(e['serializedData'])))
          .toList()
        : null,
      _createdAt = json['createdAt'] != null ? TemporalDateTime.fromString(json['createdAt']) : null,
      _updatedAt = json['updatedAt'] != null ? TemporalDateTime.fromString(json['updatedAt']) : null;
  
  ...
}

I want to create an extension class GaitPatient. But, I am running into this error,

I am currently looking at using Patient.fromJSON constructor, but a bit hesitant about using that because it doesn't seem to be the cleanest... what are my options here?

Feb 16, 2023 in AWS by Ashwini
• 5,430 points
336 views

1 answer to this question.

0 votes

it seems like the issue is that you are trying to call a named constructor GaitPatient._internal that does not exist. To resolve this error, you need to define a named constructor _internal in your GaitPatient class.

However, instead of defining a new _internal constructor in GaitPatient, you can use the existing Patient._internal constructor by extending the Patient class and adding additional properties and factory constructors to the subclass. Here's an example of how you could do this:

class GaitPatient extends Patient {
  final int strideLength;

  GaitPatient._internal({required String id, required String name, required String family_name, required TemporalDate dob, required Gender gender, required double height_in_inches, List<Assessment>? Assessments, TemporalDateTime? createdAt, TemporalDateTime? updatedAt, required this.strideLength}) 
    : super._internal(id: id, name: name, family_name: family_name, dob: dob, gender: gender, height_in_inches: height_in_inches, Assessments: Assessments, createdAt: createdAt, updatedAt: updatedAt);

  factory GaitPatient({String? id, required String name, required String family_name, required TemporalDate dob, required Gender gender, required double height_in_inches, List<Assessment>? Assessments, TemporalDateTime? createdAt, TemporalDateTime? updatedAt, required int strideLength}) {
    return GaitPatient._internal(
      id: id == null ? UUID.getUUID() : id,
      name: name,
      family_name: family_name,
      dob: dob,
      gender: gender,
      height_in_inches: height_in_inches,
      Assessments: Assessments != null ? List<Assessment>.unmodifiable(Assessments) : Assessments,
      createdAt: createdAt,
      updatedAt: updatedAt,
      strideLength: strideLength,
    );
  }

  // Add additional factory constructors or methods here
}

In this example, we define a new class GaitPatient that extends the existing Patient class. We define a new property strideLength that is specific to GaitPatient, and we define a new named constructor GaitPatient._internal that calls the superclass constructor Patient._internal.

We also define a new factory constructor GaitPatient that calls the GaitPatient._internal constructor, passing in the new strideLength parameter as well as any other parameters that are required by the superclass constructor.

You can add additional factory constructors or methods to GaitPatient as needed. This approach allows you to reuse the existing implementation of Patient while also adding new properties and methods to the subclass.

Ready to level up your coding game? Uncover the realm of microservices with our cutting-edge Microservices Developer Certification!

answered Feb 17, 2023 by sarit
• 1,830 points

Related Questions In AWS

+1 vote
5 answers

Can Celery be used with Amazon SQS

I regenerated the credentials in the IAM ...READ MORE

answered Oct 25, 2018 in AWS by triedntested
3,465 views
+1 vote
10 answers
+1 vote
2 answers

Starting with an AWS Instance with API and AUTHPARAMS

The API is usually much easier to ...READ MORE

answered Apr 17, 2018 in AWS by Cloud gunner
• 4,670 points
3,470 views
0 votes
1 answer

What is the difference between functions and classes to create reusable widgets?

In Flutter, you can create reusable widgets ...READ MORE

answered Mar 24, 2023 in Flutter by vinayak
873 views
0 votes
1 answer

Python class inherits object

Python 3.x: class MyClass(object): = new-style class class MyClass: = new-style ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 58,090 points
600 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,440 points
3,223 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,440 points
793 views
0 votes
1 answer

Problem with connecting frontend and backend services through internal load balancer

It sounds like the communication between the ...READ MORE

answered Feb 17, 2023 in AWS by sarit
• 1,830 points
465 views
0 votes
1 answer

Delivering live streaming video with CloudFront and AWS Media Services

To connect your MediaPackage channel to CloudFront, ...READ MORE

answered Feb 17, 2023 in AWS by sarit
• 1,830 points
576 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP