如何从扑朔迷离的实时数据库中获取名称和电话号码?

发布于 2025-02-13 20:35:24 字数 592 浏览 0 评论 0原文

我的数据库中有一个用户:

Users { 
    userId: Xx1j9Pih4BPnu01vnFdMfZqGOr02: {name: 'jack5' ,phone: '0845204281'
  }
}

到目前为止,我拥有以下功能,可以从实时Firebase数据库中获取数据。

static Future<dynamic> getCurrentUserInfo() async {
  String? userId = FirebaseAuth.instance.currentUser?.uid;
  final ref = FirebaseDatabase.instance.ref();
  final snapshot = await ref.child('users/$userId').get();
  if (snapshot.exists) {
    return snapshot.value;
  } else {
    print('No data available.');
    return '';
  }

该功能返回对象。如何将此对象转换为字符串?或者:如何仅获取数据库当前用户的名称?

I have one user in my database:

Users { 
    userId: Xx1j9Pih4BPnu01vnFdMfZqGOr02: {name: 'jack5' ,phone: '0845204281'
  }
}

So far I have the following function for getting data from the realtime firebase database.

static Future<dynamic> getCurrentUserInfo() async {
  String? userId = FirebaseAuth.instance.currentUser?.uid;
  final ref = FirebaseDatabase.instance.ref();
  final snapshot = await ref.child('users/$userId').get();
  if (snapshot.exists) {
    return snapshot.value;
  } else {
    print('No data available.');
    return '';
  }

The function returns an object. How do I convert this object into a string? Or: How do I simply get the name of the current user of my database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2025-02-20 20:35:24
static Future<dynamic> getCurrentUserInfo() async {
  String? userId = FirebaseAuth.instance.currentUser?.uid;
  final ref = FirebaseDatabase.instance.ref();
  final snapshot = await ref.child('users/$userId').get();
  if (snapshot.exists) {
    Map<dynamic, dynamic> values = needsSnapshot.value;
    values.forEach((key, values) {
      print(values['name']);
      print(values['phone']);
    });
  } else {
    print('No data available.');
    return '';
  }
}
static Future<dynamic> getCurrentUserInfo() async {
  String? userId = FirebaseAuth.instance.currentUser?.uid;
  final ref = FirebaseDatabase.instance.ref();
  final snapshot = await ref.child('users/$userId').get();
  if (snapshot.exists) {
    Map<dynamic, dynamic> values = needsSnapshot.value;
    values.forEach((key, values) {
      print(values['name']);
      print(values['phone']);
    });
  } else {
    print('No data available.');
    return '';
  }
}
驱逐舰岛风号 2025-02-20 20:35:24

如果您只想获取用户的name属性,假设您的用户是由UID存储的,那将是:

final snapshot = await ref.child('users/$userId/name').get();
print(snapshot.value);

If you just want to get the name property of the user, assuming your users are stored by their UID, that'd be:

final snapshot = await ref.child('users/$userId/name').get();
print(snapshot.value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文