使用提供程序显示动态映射字符串中的用户数据

Display User Data From Map String Dynamic Using Provider

我正在尝试在另一个页面上显示数据我正在使用提供者获取用户数据并将其放入我的模型中,如下所示:

 getUserData() async {
User user = FirebaseAuth.instance.currentUser!;
String value = user.uid;
return usersFirebaseReference.doc(value).snapshots().listen((event) {

  if (event.data() != null) {
    log("$event.data()");
    final Map<String, dynamic> _userData = event.data() as Map<String, dynamic>;
    userData = UserData.fromMap(_userData);

    userData!.firebaseId;
  } else {
    print('User Data is Empty');
    userData = null;
  }

  notifyListeners();

  print('Update gotten from User Data');
});

} 每当我尝试在主页中显示数据时,我都会从返回的数据中获取 Nul: userDataCrudService.userData?.store

请问我做错了什么吗?我是 flutter 和 firestore 的新手。谢谢。

下面是我的模型:

class UserData {
  bool? admin;
  String? firebaseId;
  String? username;
  String? email;
  String? password;
  String? name;
  String? store;
  String? address;
  String? phone;

  UserData({
    this.admin,
    this.firebaseId,
    this.username,
    this.email,
    this.password,
    this.name,
    this.store,
    this.address,
    this.phone,

  });

  Map<String, dynamic> toMap() {
    return {
      'admin': admin,
      'firebaseId': firebaseId,
      'username': username,
      'email': email,
      'password': password,
      'name': name,
      'store': store,
      'address': address,
      'phone': phone,

    };
  }

  factory UserData.fromMap(Map<String, dynamic> map) {
    return UserData(
      admin: map['admin'],
      firebaseId: map['firebaseId'],
      username: map['username'],
      email: map['email'],
      password: map['password'],
      name: map['name'],
      store: map['store'],
      address: map['address'],
      phone: map['phone'],

    );
  }

  String toJson() => json.encode(toMap());

  factory UserData.fromJson(String source) =>
      UserData.fromMap(json.decode(source));
}

我明白了,这是我的一个错误,我正在尝试从 firestore 获取当前用户 ID,但它使用自己的文档 ID 保存,我通过在注册时使用用户 ID 保存文档 ID 解决了这个问题。