Firebase 在查询快照上获取null
我想在流查询中得到一个空返回。有趣的是,数据通过了,但在处理数据以便在应用程序中使用时,数据在某个地方丢失了。我知道我可能在某个地方犯了一个愚蠢的错误,但我已经关注这个问题三天了。请帮忙 这是小溪Firebase 在查询快照上获取null,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我想在流查询中得到一个空返回。有趣的是,数据通过了,但在处理数据以便在应用程序中使用时,数据在某个地方丢失了。我知道我可能在某个地方犯了一个愚蠢的错误,但我已经关注这个问题三天了。请帮忙 这是小溪 Stream <SellerProfile> get sellerProfile { return sellerProfileCollection.document(uid).snapshots() .map(yieldSellerProfile); }
Stream <SellerProfile> get sellerProfile {
return sellerProfileCollection.document(uid).snapshots()
.map(yieldSellerProfile);
}
SellerProfile yieldSellerProfile(DocumentSnapshot snapshot) {
print(snapshot.data['shopName']);
return SellerProfile(
shopName: snapshot.data['shopName'] ?? '',
phone: snapshot.data['phone']??'',
credit: snapshot.data['credit'] ?? '',
posts: snapshot.data['posts'] ?? '',
sales: snapshot.data['sales'] ?? '',
avatarUrl: snapshot.data['avatarUrl'] ?? '',
location:snapshot.data['location'] ?? '',
rating: snapshot.data['rating'] ?? '',
joinedDate: snapshot.data['joinedDate'] ?? '',
);
}
这意味着数据来自firestore,但当我尝试访问前端上的数据时,我收到一个空值
这是前端
Widget build(BuildContext context) {
final AuthService _auth = AuthService();
final user = Provider.of<User>(context);
return StreamBuilder<SellerProfile>(
stream: SellerDatabaseService(uid: user.uid).sellerProfile,
builder: (context, snapshot) {
SellerProfile profile=snapshot.data;
print(profile);
return Scaffold(
backgroundColor: Colors.white,
appBar: header(context,strTitle: "Profile"),
body: SingleChildScrollView(),
);
}
);
}
我哪里做错了?提前谢谢 您没有检查快照是否有数据。使用
AsyncSnapshot
的hasData
属性添加对此的检查:
返回StreamBuilder(
流:SellerDatabaseService(uid:user.uid).sellerProfile,
生成器:(上下文,快照){
if(snapshot.hasError){
返回文本(snapshot.error.toString());
}
如果(!snapshot.hasData){//检查快照是否实际有数据
返回循环ProgressIndicator();
}
SellerProfile profile=snapshot.data;
打印(个人资料);
返回脚手架(
背景颜色:Colors.white,
appBar:header(上下文,标题:“Profile”),
正文:SingleChildScrollView(),
);
}
);
理想情况下,您还应该检查它是否有错误,如果您想对显示内容进行更精确的控制,可以使用
connectionState
来解决问题。我试图用streambuilder构建流,而不是从提供者返回流
所以我改变了这个
Widget build(BuildContext context) {
final AuthService _auth = AuthService();
final user = Provider.of<User>(context);
return StreamBuilder<SellerProfile>(
stream: SellerDatabaseService(uid: user.uid).sellerProfile,
builder: (context, snapshot) {
SellerProfile profile=snapshot.data;
print(profile);
return Scaffold(
backgroundColor: Colors.white,
appBar: header(context,strTitle: "Profile"),
body: SingleChildScrollView(),
);
}
);
}
小部件构建(构建上下文){
最终AuthService_auth=AuthService();
最终用户=提供者(上下文);
返回流生成器(
流:SellerDatabaseService(uid:user.uid).sellerProfile,
生成器:(上下文,快照){
SellerProfile profile=snapshot.data;
打印(个人资料);
返回脚手架(
背景颜色:Colors.white,
appBar:header(上下文,标题:“Profile”),
正文:SingleChildScrollView(),
);
}
);
}
对此
return StreamProvider<BuyerProfile>.value(
value: BuyerDatabaseService(uid: user.uid).buyerProfile,
builder: (context, snapshot) {
BuyerProfile profile=Provider.of<BuyerProfile>(context);
if(profile!=null){
return Scaffold(...
返回StreamProvider.value(
值:BuyerDatabaseService(uid:user.uid).buyerProfile,
生成器:(上下文,快照){
BuyerProfile profile=Provider.of(上下文);
if(profile!=null){
返回脚手架(。。。
但这并不能改变数据无法到达前端的问题。这只是一个问题check@Bright你说“不到前端”是什么意思。这是前端。已查询firestore中的数据,但无法在Ui上显示rather@Bright是的,您还没有检查数据是否真的在快照中。不包括此检查是一个主要问题。总是会出现这样的情况,snapshot.hasData
为false
,而您没有处理它。我看到了我们的观点,而我的观点是,这仍然不能解决问题。
Widget build(BuildContext context) {
final AuthService _auth = AuthService();
final user = Provider.of<User>(context);
return StreamBuilder<SellerProfile>(
stream: SellerDatabaseService(uid: user.uid).sellerProfile,
builder: (context, snapshot) {
SellerProfile profile=snapshot.data;
print(profile);
return Scaffold(
backgroundColor: Colors.white,
appBar: header(context,strTitle: "Profile"),
body: SingleChildScrollView(),
);
}
);
}
return StreamProvider<BuyerProfile>.value(
value: BuyerDatabaseService(uid: user.uid).buyerProfile,
builder: (context, snapshot) {
BuyerProfile profile=Provider.of<BuyerProfile>(context);
if(profile!=null){
return Scaffold(...