我如何使用flifter访问Firebase数据库
当我的用户登录到应用程序时,我试图用与登录用户的uid相同的信息填充我的CurrentUser对象 我的数据库服务:我如何使用flifter访问Firebase数据库,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,当我的用户登录到应用程序时,我试图用与登录用户的uid相同的信息填充我的CurrentUser对象 我的数据库服务: final CollectionReference userCollection = Firestore.instance.collection('users'); Future<User> getCurrentUserData(String uid) async{ var doc = userCollection.document(uid); final c
final CollectionReference userCollection =
Firestore.instance.collection('users');
Future<User> getCurrentUserData(String uid) async{
var doc = userCollection.document(uid);
final collection参考userCollection=
Firestore.instance.collection('users');
未来getCurrentUserData(字符串uid)异步{
var doc=userCollection.document(uid);
和我的主页:
class HomeScreen extends StatefulWidget {
final FirebaseUser currentUser;
HomeScreen({@required this.currentUser});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
类主屏幕扩展StatefulWidget{
最终FirebaseUser当前用户;
主屏幕({@required this.currentUser});
@凌驾
_HomeScreenState createState()=>\u HomeScreenState();
}
类_homescrenstate扩展状态{
和我当前的用户模型:
class CurrentUser {
static String name;
static String lastName;
static String uid;
static String phone;
static String addresses;
static String photoString;
static int cityId;
static int districtId;
static List<Loss> userLosses;
}
class当前用户{
静态字符串名;
静态字符串lastName;
静态字符串uid;
静态串电话;
静态字符串地址;
静态字符串光字符串;
静态int-cityId;
静态int-districtId;
静态列表用户损失;
}
但我想不出它们之间的联系
如果您使用的是firebase身份验证,那么您可以使用
FiresbaseAuth.instance.currentUser
,它将返回一个FirebaseUser对象,该对象将包含当前用户的信息。我这样想:
Future<User> getCurrentUserData(String uid)async {
var docRef = await userCollection.document(uid).get();
User currentUser = User.fromJson(docRef.data);
currentUser.uid=docRef.documentID;
return currentUser;
}
factory User.fromJson(Map<String, dynamic> json) {
return User(
name: json['Name'].toString(),
lastName: json['LastName'].toString(),
phone: json['Phone'].toString(),
photoString: json['PhotoString'].toString(),
districtId: int.parse(json['DistrictId'].toString()),
cityId: int.parse(json['CityId'].toString()),
addresses: json['Addresess'].toString());
}
未来getCurrentUserData(字符串uid)异步{
var docRef=await userCollection.document(uid.get();
User currentUser=User.fromJson(docRef.data);
currentUser.uid=docRef.documentID;
返回当前用户;
}
docRef.data是map,我只需更改我的用户类,如下所示:
Future<User> getCurrentUserData(String uid)async {
var docRef = await userCollection.document(uid).get();
User currentUser = User.fromJson(docRef.data);
currentUser.uid=docRef.documentID;
return currentUser;
}
factory User.fromJson(Map<String, dynamic> json) {
return User(
name: json['Name'].toString(),
lastName: json['LastName'].toString(),
phone: json['Phone'].toString(),
photoString: json['PhotoString'].toString(),
districtId: int.parse(json['DistrictId'].toString()),
cityId: int.parse(json['CityId'].toString()),
addresses: json['Addresess'].toString());
}
factory User.fromJson(映射json){
返回用户(
名称:json['name'].toString(),
lastName:json['lastName'].toString(),
phone:json['phone'].toString(),
photoString:json['photoString'].toString(),
districtId:int.parse(json['districtId'].toString()),
cityId:int.parse(json['cityId'].toString()),
地址:json['Addresess'].toString());
}
我无法像这样访问我的“用户”文档