Adi*_*VSM 6 dart firebase flutter google-cloud-firestore
StreamProvider.value 的初始数据应该是什么
我无法将其初始化为 null
Widget build(BuildContext context) {
return StreamProvider<QuerySnapshot>.value(
value: DatabaseService.withoutUID().brews,
initialData: null,
......
Run Code Online (Sandbox Code Playgroud)
initialData:null 显示错误:
The argument type 'Null' can't be assigned to the parameter type 'QuerySnapshot<Object?>'.
下面是 DatabaseService 辅助类
class DatabaseService{
final String uid;
DatabaseService(this.uid);
DatabaseService.withoutUID():uid="" ;
//collection reference
final CollectionReference brewCollection = FirebaseFirestore.instance.collection('brews');
Future updateUserData(String sugars, String name, int strength) async{
return await brewCollection.doc(uid).set({
"sugars": sugars,
"name" : name,
"strength" : strength,
});
}
//get brews stream
Stream<QuerySnapshot>? get brews{
return brewCollection.snapshots();
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里调用该流
class Home extends StatelessWidget {
final AuthService _auth = AuthService();
@override
Widget build(BuildContext context) {
return StreamProvider<QuerySnapshot>.value(
value: DatabaseService.withoutUID().brews,
initialData: null,
child: Scaffold(
backgroundColor: Colors.brown[50],
appBar: AppBar(
backgroundColor: Colors.brown[400],
title: Text("Brew Crew"),
elevation: 0.0,
actions: [
ElevatedButton.icon(
onPressed: () async {
await _auth.signOut();
}, .......
Run Code Online (Sandbox Code Playgroud)
在本教程中,我关注他们正在使用 flutter 版本,initialData在我的情况下,这不是强制性的,但在我们使用时(flutter v 2.2.3)必须提供。所以我想知道我应该在那里提供什么价值。initialDataStreamProvider
顺便说一句,我正在关注yt 频道中的本教程The net ninja
我认为你应该更换QuerySnapshot with List<Brew>.
,你的initialData: []
一切都应该工作正常。
class DatabaseService {
final String uid;
DatabaseService(this.uid);
DatabaseService.withoutUID() : uid = "";
//collection reference
final CollectionReference brewCollection =
FirebaseFirestore.instance.collection('brews');
Future updateUserData(String sugars, String name, int strength) async {
return await brewCollection.doc(uid).set({
"sugars": sugars,
"name": name,
"strength": strength,
});
}
//get brews stream
Stream<Brew>? get brews {
return brewCollection.snapshots();
}
List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
//print(doc.data);
return Brew(
name: doc.data['name'] ?? '',
strength: doc.data['strength'] ?? 0,
sugars: doc.data['sugars'] ?? '0');
}).toList();
}
}
Run Code Online (Sandbox Code Playgroud)
你的家.dart
class Home extends StatelessWidget {
final AuthService _auth = AuthService();
@override
Widget build(BuildContext context) {
return StreamProvider<List<Brew>>.value(
value: DatabaseService.withoutUID().brews,
initialData: [],
child: Scaffold(
backgroundColor: Colors.brown[50],
appBar: AppBar(
backgroundColor: Colors.brown[400],
title: Text("Brew Crew"),
elevation: 0.0,
actions: [
ElevatedButton.icon(
onPressed: () async {
await _auth.signOut();
}, .......
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6810 次 |
| 最近记录: |