无法访问 streambuilder 中的文档
Unable to access the documents in streambuilder
大家好,我试图构建一个 streambuilder 来访问我的云存储,但我无法做到 streamsnapshot.data.doc,因为我无法将文档用于我的应用程序
这是我的 streambuilder 代码:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class ChatScreen extends StatelessWidget {
const ChatScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: FirebaseFirestore
.instance
.collection('chats')
.snapshots(),
builder: (ctx, streamSnapshot) {
if (streamSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
itemCount: streamSnapshot.data, **//not able to add .documents.length after this**
itemBuilder: (ctx, index) => Container(
padding: EdgeInsets.all(8.0),
child: Text('This is working'),
));
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
);
}
}
您必须像这样在 StreamBuilder 上添加对象的类型:
StreamBuilder<YourObject>
之后,您将从 streamSnapshot
访问 documents
大家好,我试图构建一个 streambuilder 来访问我的云存储,但我无法做到 streamsnapshot.data.doc,因为我无法将文档用于我的应用程序
这是我的 streambuilder 代码:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class ChatScreen extends StatelessWidget {
const ChatScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder(
stream: FirebaseFirestore
.instance
.collection('chats')
.snapshots(),
builder: (ctx, streamSnapshot) {
if (streamSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
itemCount: streamSnapshot.data, **//not able to add .documents.length after this**
itemBuilder: (ctx, index) => Container(
padding: EdgeInsets.all(8.0),
child: Text('This is working'),
));
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
);
}
}
您必须像这样在 StreamBuilder 上添加对象的类型:
StreamBuilder<YourObject>
之后,您将从 streamSnapshot
documents