【问题标题】:How can i assign the current state user id to database reference and then database reference to future builder?如何将当前状态用户 ID 分配给数据库引用,然后将数据库引用分配给未来的构建器?
【发布时间】:2020-04-01 21:46:57
【问题描述】:

从下面的代码中,我无法在没有等待的情况下访问当前状态用户的用户 ID。如果我需要像下面那样使用等待,我需要将代码包含在一个方法中,以便异步。但是,由于代码包含在方法中,我无法访问 databaseReference 中的用户 ID 变量。 你能帮忙吗:

somemethod() async{
    FirebaseUser userid = await FirebaseAuth.instance.currentUser();
}

    final **databaseReference** = FirebaseDatabase.instance.reference().child("UserProfile").child(userid.uid).("Favorites");

@override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: **databaseReference**.once(),
        builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
          if (snapshot.hasData) {
            List<Map<dynamic, dynamic>> list = [];
            for (String key in snapshot.data.value.keys) {
              list.add(snapshot.data.value[key]);
            }

从这段代码中,我希望在 databaseReference 中访问方法中包含的用户 ID,以便我可以在 FutureBuilder 中使用 databaseReference 来检索用户信息。

谢谢。

【问题讨论】:

    标签: flutter firebase-realtime-database dart


    【解决方案1】:

    如果您希望只有 1 个未来的构建器,您需要在 databaseReference.once() 方法上传递函数参数

    class yourDatabase {
    
       Future<dynamic> once(Function somemethod) async{
           //Now, you are caling the funcion on yourDatabase class
           dynamic returnOfSomethod = await somemethod();
    
           //TODO your somemethod here
    
       }
    }
    

    叫你做这个

    somemethod() async{
        FirebaseUser userid = await FirebaseAuth.instance.currentUser();
    }
    
    //Inside the build...
    FutureBuilder(
       future: **databaseReference**.once(somemethod), // ! Dont place ()
       builder: () {}
    )
    

    希望它有效!

    【讨论】:

    • 因为它是一个数据库查询,我认为不能将一个方法作为参数放置到 once()。我试过了,还是不行。
    【解决方案2】:
            FirebaseUser userId; 
            Future databaseReference;
             somemethod() async{
                    userid = await FirebaseAuth.instance.currentUser();
                    databaseReference = FirebaseDatabase.instance.reference().child("UserProfile").child(userid.uid).("Favorites");    }
    
                @override
                  Widget build(BuildContext context) {
                    return FutureBuilder(
                        future: **databaseReference**.once(),
                        builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
                          if (snapshot.hasData) {
                            List<Map<dynamic, dynamic>> list = [];
                            for (String key in snapshot.data.value.keys) {
                              list.add(snapshot.data.value[key]);
                            }
    

    【讨论】:

    • 这些是我在这样实现时遇到的错误。getter '(' 没有为类'DatabaseReference'定义。尝试导入定义'('的库,将名称更正为现有 getter 的名称,或定义名为 '(' 的 getter 或字段。需要一个标识符。方法 'once' 没有为类 'Future' 定义。尝试将名称更正为现有方法的名称,或定义一个名为“once”的方法。
    猜你喜欢
    • 2018-02-05
    • 2014-04-07
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 2018-01-08
    • 2019-04-22
    相关资源
    最近更新 更多