Skip to content

Instantly share code, notes, and snippets.

@tcf909
Last active March 24, 2022 22:29
Show Gist options
  • Save tcf909/312c86777a9f11489b91e5fbe90d0eb1 to your computer and use it in GitHub Desktop.
Save tcf909/312c86777a9f11489b91e5fbe90d0eb1 to your computer and use it in GitHub Desktop.
Type Error
typedef ModelConstructor<T1 extends Model<T1>> = T1 Function({
ModelDefinition<T1>? dataType,
DocumentReference<Map<String, dynamic>?>? docRef,
Data? parent,
});
typedef RepositoryConstructor<T1 extends Model<T1>> = Repository<T1> Function();
final Map<Type, ModelConstructor<Model<dynamic>>>
_modelConstructorsByModelType = {};
register<U1 extends Repository<U2>, U2 extends Model<U2>>(
RepositoryConstructor<U2> repoConstructor,
ModelConstructor<U2> modelConstructor,
) {
if (_modelConstructorsByModelType.containsKey(U2)) {
throw Exception('Model type already registered.');
}
_modelConstructorsByModelType[U2] = modelConstructor;
//_modelConstructorsByModelType[U2] = modelConstructor as ModelConstructor<Model<dynamic>>; // This does not work either (it compiles, but throws a runtime error)
}
class ModelDefinition<T1 extends Model<T1>> {}
class DocumentReference<T1> {}
class Data {}
abstract class Model<T1 extends Model<T1>> {}
abstract class Repository<T1 extends Model<T1>> {}
main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment