Converting generic type to object to access its functions

65 views Asked by At

i am trying to convert the given generic model to make a collection reference return function where i can query the data

my base model

abstract class BaseModel<R> {
  String? createdAt;
  String? updatedAt;
  bool? isDisabled;
  bool? isDeleted;

  fromFirestore(
    DocumentSnapshot<JSON> snapshot,
    SnapshotOptions? options,
  ) {}

  toFirestore() {}

  toJson() {}
}

collection ref to convert to firestore

 CollectionReference<R> collections<R extends BaseModel>(
      {required CollectionNames path}) {
    var ref = fireStore.collection(path.toPathStrings());
    return ref.withConverter(
      fromFirestore: (snapshot, _) =>
          convertTypesToObjects<R>().fromFirestore(snapshot, _),
      toFirestore: (model, _) => model.toFirestore(),
    );
  }

here iam using convertTypesToObject function manually to convert the generic type to model object

dynamic convertTypesToObjects<R>() {
    switch (R) {
      case ClinicModel:
        return ClinicModel();
      default:
        throw AuthException(message: "Model Does not Exit");
    }
  }

await collections\<ClinicModel\>(path: CollectionNames.clinic);

can i make this dynamic i want to do somthing like this


var a = R();

 return ref.withConverter(
      fromFirestore: (snapshot, _) =>
          ***a.fromFirestore(snapshot, _),***
      toFirestore: (model, _) => model.toFirestore(),
    );
0

There are 0 answers