如何根据 uid 创建用户配置文件并导航到配置文件页面?
How to create a user profile based on uid and navigate to the profile page?
我创建了一个使用 firebase 身份验证的用户,它也有自己的数据库。
用户可以向其他用户发送好友请求,发送者uid保存在接收者用户的请求集合中。在我的应用程序中,我可以列出请求。
所以我有用户的 uid,当我按下眼睛图标时,我想导航到另一个页面,该页面将是用户的个人资料。所以我需要像 UserProfilePage class 这样的东西。这是我的代码:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_auths/pages/profilepage.dart';
import 'package:flutter_auths/services/authentications.dart';
import 'package:flutter_auths/main.dart';
class RequestPage extends StatefulWidget {
final String uid;
RequestPage({Key key, @required this.uid}) : super(key: key);
@override
_RequestPageState createState() => _RequestPageState(uid);
}
class _RequestPageState extends State<RequestPage> {
final String uid;
_RequestPageState(this.uid);
var taskcollections = Firestore.instance.collection('users');
String task;
void acceptFriendRequest(String theUid) async {
await Firestore.instance
.collection('users')
.document(uid)
.collection('friendships')
.document()
.setData({
'FriendUid': theUid,
}).then((onValue) {
print('current user: ');
print(uid);
print(theUid);
});
await Firestore.instance
.collection('users')
.document(theUid)
.collection('friendships')
.document()
.setData({
'FriendUid': uid,
}).then((onValue) {
});
await Firestore.instance
.collection('users')
.document(uid)
.collection('requests')
.document(theUid)
.delete();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () { },
child: Icon(Icons.add),
),
appBar: AppBar(
title: Text(
"Requests",
style: TextStyle(
fontFamily: "tepeno",
fontWeight: FontWeight.w600,
),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.exit_to_app),
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onPressed: () => signOutUser().then((value) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => HomePage()),
(Route<dynamic> route) => false);
}),
),
],
),
body: StreamBuilder<QuerySnapshot>(
stream: taskcollections
.document(uid)
.collection('requests')
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot ds = snapshot.data.documents[index];
return Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(5.0),
),
margin: EdgeInsets.all(8.0),
child: ListTile(
title: Text(
ds['FriendUid'] ?? '',
style: TextStyle(
fontFamily: "tepeno",
fontSize: 18.0,
color: Colors.white,
),
),
trailing: Wrap(
spacing: 7, // space between two icons
children: <Widget>[
IconButton(
icon: Icon(
Icons.check,
size: 27.0,
color: Colors.brown[900],
),
onPressed: () {
acceptFriendRequest(ds['FriendUid']);
},
),
IconButton(
icon: Icon(
Icons.do_not_disturb,
size: 27.0,
color: Colors.brown[900],
),
onPressed: () {
// _onDeleteItemPressed(index);
},
),
IconButton(
icon: Icon(
Icons.remove_red_eye,
size: 27.0,
color: Colors.brown[900],
),
onPressed: () {
},
),
],
),
),
);
},
);
} else if (snapshot.hasError) {
return CircularProgressIndicator();
} else {
return CircularProgressIndicator();
}
},
),
);
}
}
我是 Flutter 的初学者。谢谢你的帮助。
您必须执行另一个查询,因为 Firestore 中的查询很浅,这意味着如果您指向集合 requests
,您将无法获得集合 users
中的详细信息。所以你必须获取用户详细信息,然后将它们发送到 UserProfilePage
页面:
onPressed: () async {
DocumentSnapshot result = await Firestore.instance.collection('users').document(uid).get();
Navigator.push(
context,
MaterialPageRoute(builder: (context) => UserProfilePage(userInfo : result)),
);
},
我创建了一个使用 firebase 身份验证的用户,它也有自己的数据库。
用户可以向其他用户发送好友请求,发送者uid保存在接收者用户的请求集合中。在我的应用程序中,我可以列出请求。
所以我有用户的 uid,当我按下眼睛图标时,我想导航到另一个页面,该页面将是用户的个人资料。所以我需要像 UserProfilePage class 这样的东西。这是我的代码:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_auths/pages/profilepage.dart';
import 'package:flutter_auths/services/authentications.dart';
import 'package:flutter_auths/main.dart';
class RequestPage extends StatefulWidget {
final String uid;
RequestPage({Key key, @required this.uid}) : super(key: key);
@override
_RequestPageState createState() => _RequestPageState(uid);
}
class _RequestPageState extends State<RequestPage> {
final String uid;
_RequestPageState(this.uid);
var taskcollections = Firestore.instance.collection('users');
String task;
void acceptFriendRequest(String theUid) async {
await Firestore.instance
.collection('users')
.document(uid)
.collection('friendships')
.document()
.setData({
'FriendUid': theUid,
}).then((onValue) {
print('current user: ');
print(uid);
print(theUid);
});
await Firestore.instance
.collection('users')
.document(theUid)
.collection('friendships')
.document()
.setData({
'FriendUid': uid,
}).then((onValue) {
});
await Firestore.instance
.collection('users')
.document(uid)
.collection('requests')
.document(theUid)
.delete();
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () { },
child: Icon(Icons.add),
),
appBar: AppBar(
title: Text(
"Requests",
style: TextStyle(
fontFamily: "tepeno",
fontWeight: FontWeight.w600,
),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.exit_to_app),
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onPressed: () => signOutUser().then((value) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => HomePage()),
(Route<dynamic> route) => false);
}),
),
],
),
body: StreamBuilder<QuerySnapshot>(
stream: taskcollections
.document(uid)
.collection('requests')
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot ds = snapshot.data.documents[index];
return Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(5.0),
),
margin: EdgeInsets.all(8.0),
child: ListTile(
title: Text(
ds['FriendUid'] ?? '',
style: TextStyle(
fontFamily: "tepeno",
fontSize: 18.0,
color: Colors.white,
),
),
trailing: Wrap(
spacing: 7, // space between two icons
children: <Widget>[
IconButton(
icon: Icon(
Icons.check,
size: 27.0,
color: Colors.brown[900],
),
onPressed: () {
acceptFriendRequest(ds['FriendUid']);
},
),
IconButton(
icon: Icon(
Icons.do_not_disturb,
size: 27.0,
color: Colors.brown[900],
),
onPressed: () {
// _onDeleteItemPressed(index);
},
),
IconButton(
icon: Icon(
Icons.remove_red_eye,
size: 27.0,
color: Colors.brown[900],
),
onPressed: () {
},
),
],
),
),
);
},
);
} else if (snapshot.hasError) {
return CircularProgressIndicator();
} else {
return CircularProgressIndicator();
}
},
),
);
}
}
我是 Flutter 的初学者。谢谢你的帮助。
您必须执行另一个查询,因为 Firestore 中的查询很浅,这意味着如果您指向集合 requests
,您将无法获得集合 users
中的详细信息。所以你必须获取用户详细信息,然后将它们发送到 UserProfilePage
页面:
onPressed: () async {
DocumentSnapshot result = await Firestore.instance.collection('users').document(uid).get();
Navigator.push(
context,
MaterialPageRoute(builder: (context) => UserProfilePage(userInfo : result)),
);
},