【问题标题】:How can i reach data Firebase with Flutter如何使用 Flutter 访问数据 Firebase
【发布时间】:2020-10-10 14:37:42
【问题描述】:

当我的用户登录应用程序时,我正在尝试使用与登录用户的 uid 相同的信息填充我的 CurrentUser 对象

我的数据库服务:

final CollectionReference userCollection =
  Firestore.instance.collection('users');

Future<User> getCurrentUserData(String uid) async{
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> {

还有我的 CurrentUser 模型:

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;
}

但我无法弄清楚它们之间的联系

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    如果您使用的是 firebase 身份验证,那么您可以使用FiresbaseAuth.instance.currentUser,它将返回一个 FirebaseUser 对象,该对象将包含当前用户的信息。

    【讨论】:

    • 我无法像这样访问我的“用户”文档
    【解决方案2】:

    我是这样想的:

    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;
    }
    

    docRef.data 是 映射,我只是像这样更改我的用户类:

    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());
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-22
      • 2019-07-10
      • 2020-08-13
      • 2020-05-25
      • 2020-10-22
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      相关资源
      最近更新 更多