经过数小时的挣扎和浏览,我需要您的帮助。
我在一个实施了 Firestore 的颤振项目上。我在使用 Riverpod 的 mvvm 架构上。
我的firestore服务有这个代码:
CollectionReference getCollection(String collection) =>
_firestore.collection(collection);
getCurrentCustomer() {
String uid = _auth.currentUser.uid;
print('current customer id is : $uid');
var customerStream = getCollection('customer')
.doc(uid)
.snapshots(includeMetadataChanges: true);
print('customerStream is a $customerStream');
return customerStream;
}
我用这个提供者访问这个 FirestoreService :
final firestoreProvider = Provider<FirestoreService>((ref) {
return FirestoreService(
ref.read(authentificationServiceProvider),
);
});
在我的 CustomerProfileViewModel 上,我得到了这个:
final customerProfileViewModelProvider =
ChangeNotifierProvider<CustomerProfileViewModel>((ref) {
return CustomerProfileViewModel(
ref.read(authentificationServiceProvider),
ref.read(navigationServiceProvider),
ref.read(firestoreProvider),
);
});
class CustomerProfileViewModel extends ChangeNotifier {
final AuthentificationService _auth;
final NavigationService _navigationService;
final FirestoreService _firestore;
CustomerProfileViewModel(
this._auth, this._navigationService, this._firestore);
getCurrentCustomerInfo() {
Stream customerStream = _firestore.getCurrentCustomer();
print('customer Stream sur le model est $customerStream');
return customerStream;
}
updateCurrentUserInfo({String dataField, dataValue}) {
_firestore.updateCurrentCustomerInfo(dataField, dataValue);
notifyListeners();
}
}
最后,在作为 ConsumerWidget 的 CustomerProfileView 中,我这样称呼我的模型:
var model = watch(customerProfileViewModelProvider);
var stream = model.getCurrentCustomerInfo();
并像这样设置了我的 StreamBuilder
StreamBuilder(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot
)
然后我得到这个错误:
I/flutter ( 2609): current customer id is : 9tZTfddurwQRsSuGnH7pkPvmvvF2
I/flutter ( 2609): customerStream is a Instance of '_MapStream<DocumentSnapshotPlatform, DocumentSnapshot>'
I/flutter ( 2609): customer Stream sur le model est Instance of '_MapStream<DocumentSnapshotPlatform, DocumentSnapshot>'
I/flutter ( 2609): stream view est Instance of '_MapStream<DocumentSnapshotPlatform, DocumentSnapshot>'
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following _TypeError was thrown building CustomerProfileView(dirty, dependencies: [UncontrolledProviderScope], state: _ConsumerState#b4319):
type '_MapStream<DocumentSnapshotPlatform, DocumentSnapshot>' is not a subtype of type 'Stream<QuerySnapshot>'
The relevant error-causing widget was:
CustomerProfileView file:///Users/Alex/AndroidStudioProjects/clickncollect/lib/ui/views/home/home_viewmodel.dart:35:16
When the exception was thrown, this was the stack:
#0 CustomerProfileView.build (package:clickncollect_app/ui/views/customer/customer_profile/customer_profile_view.dart:40:25)
#1 _ConsumerState.build (package:flutter_riverpod/src/consumer.dart:300:35)
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)
#4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
...
════════════════════════════════════════════════════════════════════════════════════════════════
它使用 Future 进行所有相同的工作,使用 collection('customers').doc(uid).get() 查询并使用 FutureBuilder 构建它。
请帮助我,我从未见过这种 MapStream 类型,互联网也没有(经过数小时的搜索)。
谢谢阅读 !