【问题标题】:I want to fetch current user data from firebase database in my flutter application我想在我的颤振应用程序中从 firebase 数据库中获取当前用户数据
【发布时间】:2020-08-01 10:11:41
【问题描述】:

我的问题是我只想获取当前用户的数据。但下面的代码正在获取我的数据库中存在的所有用户的数据。我怎样才能获取唯一且唯一的当前用户的数据。

  1. 这是我从 firebase 获取数据的代码(我使用的是实时数据库)。 在这个“节点名称”中是存储我的数据的字段。
class ShowDataPage extends StatefulWidget {
  @override
  _ShowDataPageState createState() => _ShowDataPageState();
}

class _ShowDataPageState extends State<ShowDataPage> {
  List<myData> allData = [];

  @override
  void initState() {
    DatabaseReference ref = FirebaseDatabase.instance.reference();
    ref.child('node-name').once().then((DataSnapshot snap) {
      var keys = snap.value.keys;
      var data = snap.value;
      allData.clear();
      for (var key in keys) {
        myData d = new myData(
          data[key]['name'],
          data[key]['message'],
          data[key]['profession'],
        );
        allData.add(d);
      }
      setState(() {
        print('Length : ${allData.length}');
      });
    });
  }
  1. 这是我以“节点名称”的名义将数据上传到 Firebase 的代码。此代码存储在另一个文件中,并且还有另一个必要字段,但这是将我的数据上传到 firebase 的字段。
_sendToServer() {
    if (_key.currentState.validate()) {
      _key.currentState.save();
      DatabaseReference ref = FirebaseDatabase.instance.reference();
      var data = {
        "name": name,
        "profession": profession,
        "message": message,
      };
      ref.child('node-name').push().set(data).then((v) {
        _key.currentState.reset();
      });
    } else {
      setState(() {
        _autovalidate = true;
      });
    }
  }
  1. 我在 firebase 中的数据库如下所示。

【问题讨论】:

    标签: firebase flutter firebase-realtime-database dart firebase-authentication


    【解决方案1】:

    使用用户 uid:

    ref.child('node-name').child("M5CCSXQo3Upq5OC7y3lw").once()
    .then((DataSnapshot snap) {...}
    

    如果您不知道 uid 并且没有使用它,则按名称执行查询。

    @override
    void initState() {
        FirebaseAuth.instance.currentUser().then((user){
          fetchUser(user);
        });
    }
    
    fetchUser(FirebaseUser user)
    {
      DatabaseReference ref = FirebaseDatabase.instance.reference();
      ref.child('node-name').child(user.uid).once().then((DataSnapshot snap) {
          var keys = snap.value.keys;
          var data = snap.value;
          allData.clear();
          for (var key in keys) {
            myData d = new myData(
              data[key]['name'],
              data[key]['message'],
              data[key]['profession'],
            );
            allData.add(d);
          }
          setState(() {
            print('Length : ${allData.length}');
          });
        });
    }
    

    【讨论】:

    • 参数类型'future string'不能分配给参数类型'string'。当我尝试将当前用户 uid 放入其中时,会出现此错误。实际上,我也使用函数 getUserID() 从 firebase 获取 uid。
    • 只需使用:await 作为 .currentUser() 调用的前缀。或使用:.currentUser().then((user){ this.user = user });
    • 我添加了等待异步,但由于这一切都在 initState 中,所以我无法做到这一点。所以我将所有这些当前代码放在具有异步功能的小部件中,并添加了等待并从 initstate 中调用它。但我似乎没有返回任何东西。正在返回一个空白页。这可能是什么原因
    • 只需在 initState 中使用 .then 版本,然后在 .then 中调用其中包含 FirebaseDatabase 调用的函数。
    • 我更新了答案,应该是类似的。您现在应该能够看到它是如何工作的。还有许多其他方法可以解决这个问题。在这里很难解释。
    【解决方案2】:

    你可以这样使用

    ...
    ref.child('node-name').child('/** current_user_key **/').once()
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 2022-10-09
      • 2021-04-07
      相关资源
      最近更新 更多