我试图使用一个构造函数的通知,但构造函数是“未定义”的文件中,我试图使用它。
下面是provider.dart文件中定义的类:
class TrxnNotifier extends Notifier<Trxn> {
TrxnNotifier.fromFirestore(Map<String, dynamic> firestore)
: trxnId = firestore['trxnId'],
userId = firestore['userId'],
companyId = firestore['companyId'];
}
字符串
下面是我尝试在dashboard.dart文件中使用它的地方:
final trxnStreamProvider = StreamProvider.autoDispose<List<Trxn>>((ref) {
final stream = firestoreService
.getCompanyTrxns(ref.read(globalsNotifierProvider).companyId!);
return stream.map((snapshot) => snapshot.docs.map((doc) =>
ref.read(trxnNotifierProvider.notifier).fromFirestore(doc.data() as Map<String, dynamic>))
.toList());
});
型
在dashboard.dart文件中,我得到错误消息:The method 'fromFirestore' isn 't defined for the type 'TrxnNotifier'.
我该怎么做?谢谢
更新:
我就像你说的那样,但现在我得到了这个错误:
构造函数的名称必须与封闭类的名称匹配。dart(invalid_constructor_name)
下面是我现在的代码:
class TrxnNotifier extends Notifier<Trxn> {
fromFirestore(Map<String, dynamic> firestore)
: trxnId = firestore['trxnId'],
userId = firestore['userId'],
companyId = firestore['companyId'];
型
我没有像你说的那样在它周围{},但我不知道我会怎么做。
谢谢你的帮助。
1条答案
按热度按时间r6l8ljro1#
您正在定义
TrxnNotifier.fromFirestore
,它是一个命名构造函数。命名构造函数通常如下调用:字符串
你所做的是从
ref.read(trxnNotifierProvider.notifier)
调用fromFirestore
,ref.read(trxnNotifierProvider.notifier)
是TrxnNotifier
的一个示例,而不是类本身。如果你想从一个示例调用一个方法,在TrxnNotifier
类中定义一个示例方法,如下所示:型