Firebase 在访问当前用户时需要帮助';从cloud firestore获取的信息,并将这些信息包含在我的应用程序中
这是我想要访问和包括从firestore登录用户数据的用户的地方Firebase 在访问当前用户时需要帮助';从cloud firestore获取的信息,并将这些信息包含在我的应用程序中,firebase,flutter,google-cloud-firestore,firebase-authentication,google-cloud-storage,Firebase,Flutter,Google Cloud Firestore,Firebase Authentication,Google Cloud Storage,这是我想要访问和包括从firestore登录用户数据的用户的地方 class CategorySelector extends StatefulWidget { @override _CategorySelectorState createState() => _CategorySelectorState(); } class _CategorySelectorState extends State<CategorySelector> { Future<W
class CategorySelector extends StatefulWidget {
@override
_CategorySelectorState createState() => _CategorySelectorState();
}
class _CategorySelectorState extends State<CategorySelector> {
Future<Widget> _getImage(BuildContext context, String imageName) async{
Image image;
await FireStorageService.loadImage(context,imageName).then((value){
image = Image.network(value.toString(),
fit: BoxFit.cover,
);
});
return image;
}
@override
Widget build(BuildContext context) {
return Container(
height: 100.0,
width: double.infinity,
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 80.0,
height: 80.0,
margin: EdgeInsets.only(left: 10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
boxShadow: [
BoxShadow(
//color: Color.fromRGBO(143, 148, 251, .2),
color: Colors.black12,
blurRadius: 5.0,
offset: Offset(8, 8)
)
]
),
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: FutureBuilder(
future: _getImage(context,"Screenshot_2020-12-12-07-27-13-73.png"),
builder: (context, snapshot){
//if(snapshot.connectionState == ConnectionState.done){
return Container(
width: 80.0,
height: 80.0,
child: snapshot.data,
);
//}
},
),
/*child: Image(
image: AssetImage(user.imageUrl),
),*/
),),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Welcome,',
style: TextStyle(
fontFamily: 'Roboto',
color: Colors.grey,
fontSize: 20.0,
),),
Text('hi',
style: TextStyle(
fontFamily: 'Roboto',
color: Colors.black54,
fontSize: 20.0,
fontWeight: FontWeight.bold,
),),
],
),
Container(
margin: EdgeInsets.only(right: 10.0),
width: 80.0,
height: 80.0,
child: Image(
image: AssetImage('assets/images/logo.png'),
),),
],
),
);
}
}
class FireStorageService extends ChangeNotifier{
FireStorageService();
static Future<dynamic> loadImage(BuildContext context, String image) async {
return await FirebaseStorage.instance.ref().child(image).getDownloadURL();
}
}
class CategorySelector扩展StatefulWidget{
@凌驾
_CategorySelectorState createState()=>U CategorySelectorState();
}
类_CategorySelectorState扩展状态{
Future\u getImage(BuildContext上下文,字符串imageName)异步{
图像;
等待FireStorageService.loadImage(上下文,图像名称)。然后((值){
image=image.network(value.toString(),
适合:BoxFit.cover,
);
});
返回图像;
}
@凌驾
小部件构建(构建上下文){
返回容器(
高度:100.0,
宽度:double.infinity,
孩子:排(
crossAxisAlignment:crossAxisAlignment.stretch,
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
容器(
宽度:80.0,
身高:80.0,
边距:仅限边缘设置(左:10.0),
装饰:盒子装饰(
边界半径:边界半径。圆形(20.0),
boxShadow:[
箱形阴影(
//颜色:颜色。来自RGBO(143148251,.2),
颜色:颜色。黑色,
半径:5.0,
偏移量:偏移量(8,8)
)
]
),
孩子:ClipRRect(
边界半径:边界半径。圆形(20.0),
孩子:未来建设者(
未来:_getImage(上下文,“截图_2020-12-12-07-27-13-73.png”),
生成器:(上下文,快照){
//if(snapshot.connectionState==connectionState.done){
返回容器(
宽度:80.0,
身高:80.0,
子:snapshot.data,
);
//}
},
),
/*孩子:图像(
image:AssetImage(user.imageUrl),
),*/
),),
纵队(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
文本(“欢迎,”,
样式:TextStyle(
fontFamily:“机器人”,
颜色:颜色。灰色,
字体大小:20.0,
),),
文本('hi',
样式:TextStyle(
fontFamily:“机器人”,
颜色:颜色。黑色54,
字体大小:20.0,
fontWeight:fontWeight.bold,
),),
],
),
容器(
边距:仅限边集(右:10.0),
宽度:80.0,
身高:80.0,
孩子:图像(
图像:AssetImage('assets/images/logo.png'),
),),
],
),
);
}
}
类FireStorageService扩展了ChangeNotifier{
FireStorageService();
静态未来加载映像(BuildContext上下文、字符串映像)异步{
return wait wait FirebaseStorage.instance.ref().child(image.getDownloadURL();
}
}
我为用户身份验证创建了此类
class AuthenticationService {
final FirebaseAuth _auth = FirebaseAuth.instance;
//user registration with email and password
Future<User> createNewUser(String email, String password, String imageUrl) async {
try{
UserCredential result = await _auth
.createUserWithEmailAndPassword(email: email, password: password);
User user = result.user;
print('auth ser $imageUrl');
if(user != null) {
await DatabaseManager().createUserData(user.email, imageUrl, user.uid);
return user;
}
return null;
}catch(e){
return null;
}
}
//user sign in with email and password
Future loginUser(String email, String password) async {
try{
UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
return result.user;
}catch(e){
print(e.toString());
return null;
}
}
*//i think this function will help me to access current user's information, but it did not help*
Future<User> getCurrentUser()async{
User user = _auth.currentUser;
return user;
}
}
类身份验证服务{
final FirebaseAuth _auth=FirebaseAuth.instance;
//使用电子邮件和密码进行用户注册
Future createNewUser(字符串电子邮件、字符串密码、字符串图像URL)异步{
试一试{
UserCredential result=wait\u auth
.createUserWithEmailAndPassword(电子邮件:电子邮件,密码:密码);
User=result.User;
打印('auth ser$imageUrl');
如果(用户!=null){
等待DatabaseManager().createUserData(user.email、imageUrl、user.uid);
返回用户;
}
返回null;
}捕获(e){
返回null;
}
}
//用户使用电子邮件和密码登录
未来登录用户(字符串电子邮件、字符串密码)异步{
试一试{
UserCredential result=wait _auth.signin with email and password(email:email,password:password);
返回result.user;
}捕获(e){
打印(如toString());
返回null;
}
}
*//我认为这个功能将帮助我访问当前用户的信息,但它没有帮助*
未来getCurrentUser()异步{
用户用户=_auth.currentUser;
返回用户;
}
}
这是我包含所有数据库操作的地方。
class DatabaseManager {
final CollectionReference users = FirebaseFirestore.instance.collection('users');
Future<void> createUserData( String email,String imageUrl, String uid) async{
return await users.doc(uid).set({
'id': uid,
'email': email,
'user_name': email.substring(0,email.lastIndexOf("@")),
'img_url': imageUrl
});
}
}
类数据库管理器{
final CollectionReference users=FirebaseFirestore.instance.collection('users');
未来的createUserData(字符串电子邮件、字符串imageUrl、字符串uid)异步{
return wait users.doc(uid).set({
'id':uid,
“电子邮件”:电子邮件,
“用户名”:email.substring(0,email.lastIndexOf(“@”),
“img_url”:imageUrl
});
}
}
这是我的用户模型
import 'package:flutter/foundation.dart';
class User {
int id;
String name;
String email;
String imageUrl;
String password;
User({
@required this.id,
@required this.name,
@required this.imageUrl,
@required this.email,
@required this.password,
});
User.fromMap(Map<String, dynamic> json){
this.id = json['id'];
this.name = json['name'];
this.imageUrl = json['img_url'];
this.email = json['email'];
this.password = json['password'];
}
}
导入“包:flift/foundation.dart”;
类用户{
int-id;
字符串名;
字符串电子邮件;
字符串imageUrl;
字符串密码;
使用者({
@需要这个.id,
@需要此名称,
@需要此.imageUrl,
@需要此电子邮件,
@需要此密码,
});
User.fromMap(MapJSON){
this.id=json['id'];
this.name=json['name'];
this.imageUrl=json['img_url'];
this.email=json['email'];
this.password=json['password'];
}
}
我真的不知道如何从firebase检索登录用户的信息我想访问firebase存储中存储的图像和firestore中的用户名,并通过class categorySelector显示在我的应用程序中。非常感谢您的帮助。这就是我构建用户集合的方式。
如果您使用Firebase Auth,您可以访问姓名、电子邮件和电话号码等简单数据,您可以使用
FirebaseAuth.instance.currentUser
否则,如果您想访问Firestore中的数据,我建议您阅读并自行学习