【发布时间】:2022-11-11 21:24:43
【问题描述】:
我已经在用户集合中存储了数据(来自 Auth 表的每个用户的 UID)。我需要从用户那里获取数据并将其与currentSignedIn 用户的 UID 匹配。如果当前登录用户的 UID 与集合中的 UID 匹配,则页面可能会返回或导航到第 1 页,否则导航到第 2 页。
目前,仅显示用户集合中的 UID 和 currentSignedIn 用户的 UID 即可查看数据。现在我需要一种方法来匹配数据并使用 if-else 语句。
代码:
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
Future _getDatafromDatabase() async{
await FirebaseFirestore.instance.collection('users')
.doc().get().then((snapshot) async
{
if(snapshot.exists)
{
setState(() {
useruid = snapshot.data()!["useruid"];
});
}
});
}
List<Users> uidofusers = [];
@override
void initState(){
fetchRecords();
FirebaseFirestore.instance.collection('users').snapshots().listen((records) {
mapRecords(records);
});
super.initState();
}
fetchRecords() async {
var records = await FirebaseFirestore.instance.collection('users').get();
mapRecords(records);
}
mapRecords(QuerySnapshot<Map<String, dynamic>>records) {
var _list = records.docs
.map((item)=>Users(
useruid: item['useruid'],
)
).toList();
setState(() {
uidofusers = _list;
});
}
@override
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text("Update Profile", style: TextStyle(fontSize: 30.0),),
backgroundColor: Colors.blue,
actions: [
IconButton(
icon: const Icon(Icons.logout),
onPressed: () => FirebaseAuth.instance.signOut(),
),
]
),
body: ListView.builder(
itemCount: uidofusers.length,
itemBuilder: (context, index){
return ListTile(
title: Text(uidofusers[index].useruid),
subtitle: Text(uid!),
);
},
),
【问题讨论】:
标签: flutter firebase google-cloud-firestore