-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Allow static members to reference type parameters of the class #34415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
(Using a constructor instead fails with #34414.) |
(I worked around the issue by calling it |
I have the same issue:
|
@Boyfox - you want |
I am trying to create an abstract class with all the common functions so that code repetition is less. For e.g I have two model classes abstract class Database<T> {
/// constants
static final testModelName = 'TestModelName';
static final testModelName1 = 'TestModelName1';
/// constructor
Database(String collectionName) {
ref =
FirebaseFirestore.instance.collection(collectionName).withConverter<T>(
fromFirestore: (snapshot, _) => fromJson(snapshot.data()!),
toFirestore: (t, _) => toJson(),
);
}
static late final CollectionReference<T>
ref; // Static members can't reference type parameters of the class.
/// json serialization
Map<String, dynamic> toJson();
T fromJson(Map<String, dynamic> json);
/// helper function
static Future<List<T>> get() async {
// Static members can't reference type parameters of the class.
return (await ref.get()).docs.map((e) => e.data()).toList();
}
static Future<void> add(T data) async {
// Static members can't reference type parameters of the class.
await ref.add(data);
}
}
class TestModel extends Database<TestModel> {
TestModel(this.id, this.name) : super(Database.testModelName);
final String id;
final String name;
@override
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
};
@override
TestModel fromJson(Map<String, dynamic> json) =>
TestModel(json['id'], json['name']);
}
class TestModel1 extends Database<TestModel1> {
TestModel1(this.id, this.name, this.name1) : super(Database.testModelName1);
final String id;
final String name;
final String name1;
@override
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'name1': name1,
};
@override
TestModel1 fromJson(Map<String, dynamic> json) =>
TestModel1(json['id'], json['name'], json['name1']);
} |
I have a class of type
Foo<T>
on which I want to call a static method to build aFoo<T>
using a source ofR
values and a callback that can convertR
s toT
s.I would call this as
Foo<X>.convert<Y>(...)
.This fails with
Error: Static members can't reference type parameters of the class
.The text was updated successfully, but these errors were encountered: