如何在 flutter 中使用 streambuilder 从 firestrome 获取用户单个详细信息
How to get user single detail from firestrome using streambuilder in flutter
我正在尝试使用 Flutter 和 Firestore 构建应用程序。使用 StreamBuilder 加载当前用户单个文件以将其显示在文本中时,出现以下错误
════════ 组件库捕获异常═════════════════════════════════ ═══ 构建 StreamBuilder>(dirty, state:
_StreamBuilderBaseState, AsyncSnapshot>>#8a26d): 错误状态:无法获取不存在的 DocumentSnapshotPlatform 上的字段。
这是我的代码
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class Status extends StatefulWidget {
Status({Key? key}) : super(key: key);
@override
State<Status> createState() => _StatusState();
}
class _StatusState extends State<Status> {
@override
Widget build(BuildContext context) {
return StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection("Users")
.doc(FirebaseAuth.instance.currentUser!.uid)
.snapshots(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Text("Loading");
} else if (snapshot.hasError) {
return Text('Something went wrong');
} else if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
dynamic data = snapshot.data;
return new Text(data['status']);
});
}
}
你需要通过
DocumentSnapshot to AsyncSnapshot..
Try this code
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class Status extends StatefulWidget {
Status({Key? key}) : super(key: key);
@override
State<Status> createState() => _StatusState();
}
class _StatusState extends State<Status> {
final Stream<DocumentSnapshot> _usersStream = FirebaseFirestore.instance
.collection("Users")
.doc(FirebaseAuth.instance.currentUser.uid)
.snapshots()
@override
Widget build(BuildContext context) {
return StreamBuilder<DocumentSnapshot>(
stream: _usersStream,
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return Text("Loading");
} else if (snapshot.hasError) {
return Text('Something went wrong');
} else if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
dynamic data = snapshot.data;
return new Text(data['status']);
});
}
}
我正在尝试使用 Flutter 和 Firestore 构建应用程序。使用 StreamBuilder 加载当前用户单个文件以将其显示在文本中时,出现以下错误
════════ 组件库捕获异常═════════════════════════════════ ═══ 构建 StreamBuilder
这是我的代码
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class Status extends StatefulWidget {
Status({Key? key}) : super(key: key);
@override
State<Status> createState() => _StatusState();
}
class _StatusState extends State<Status> {
@override
Widget build(BuildContext context) {
return StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection("Users")
.doc(FirebaseAuth.instance.currentUser!.uid)
.snapshots(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Text("Loading");
} else if (snapshot.hasError) {
return Text('Something went wrong');
} else if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
dynamic data = snapshot.data;
return new Text(data['status']);
});
}
}
你需要通过
DocumentSnapshot to AsyncSnapshot.. Try this code
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class Status extends StatefulWidget {
Status({Key? key}) : super(key: key);
@override
State<Status> createState() => _StatusState();
}
class _StatusState extends State<Status> {
final Stream<DocumentSnapshot> _usersStream = FirebaseFirestore.instance
.collection("Users")
.doc(FirebaseAuth.instance.currentUser.uid)
.snapshots()
@override
Widget build(BuildContext context) {
return StreamBuilder<DocumentSnapshot>(
stream: _usersStream,
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return Text("Loading");
} else if (snapshot.hasError) {
return Text('Something went wrong');
} else if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
dynamic data = snapshot.data;
return new Text(data['status']);
});
}
}