【问题标题】:Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist FirebaseFirestore flutter状态不佳:无法在不存在 FirebaseFirestore 的 DocumentSnapshotPlatform 上获取字段
【发布时间】:2021-08-15 10:38:02
【问题描述】:

请帮助我是 firebase 的新手,我想获得 field Widgetid name'name in collectionName'用户'

(FirebaseFirestore.instance.collection('Users').doc(uid).collection('Widget').snapshots())

用于从另一个 collectionName 'Widget' 获取数据

( FirebaseFirestore.instance.collection('Widget').doc(type).snapshots() )

获取真实数据,抱歉我不知道如何 解释更多,请阅读代码让你明白

enter image description here

enter image description here

enter image description here

主页()

    String type,

    final _widget = FirebaseFirestore.instance
    .collection('Users')
    .doc(uid)
    .collection('Widget')
    .snapshots();
    final color = Theme.of(context).primaryColor;
    final color1 = Theme.of(context).accentColor;
    final shadow = Theme.of(context).cardColor;
   return Container(
  child: Column(
    children: [
      Text(
        'Widget Menu',
        style: Theme.of(context).textTheme.headline5,
      ),
      StreamBuilder<QuerySnapshot>(
          stream: _widget,
          builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }

            return Container(
              height: 50*SizeConfig.heightMultiplier,
              child: ListView(
                children: snapshot.data.docs.map((document) {
                  return Card(
                    color: color1,
                    shadowColor: shadow,
                    elevation: 8,
                    child: Container(
                      height: 15*SizeConfig.heightMultiplier,
                      child: Row(
                        children: [
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Card(
                              color: color1,
                              elevation: 5,
                              child: CircleAvatar(
                                backgroundImage:  document['image'] != null ?         
                                    (themeProvider.isDarkMode
                                    ? AssetImage('images/MainDark.png')
                                    : AssetImage('images/MainLight.png'))
                                    : NetworkImage(document['image']),
                              backgroundColor: color1,
                                minRadius: 50,
                              ),
                            ),
                          ),
                          Text(document['name']),
                          IconButton(icon: Icon(Icons.arrow_forward_ios_outlined),
                              onPressed: (){
                            setState(() {
                              type = document['name'];
                              print(type);
                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) =>
                                          WidgetHome()));
                            });
                          })

                        ],
                      ),
                    ),
                  );
                }).toList(),
              ),
            );
          })
        ],
      ),
    );
  }
}

小部件主页()

             Container(
                  width: double.maxFinite,
                  height: double.maxFinite,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(
                        Radius.circular(
                            2 * SizeConfig.heightMultiplier)),
                    color: color1,
                  ),
                  child: SafeArea(
                    child: Container(
                      child: Container(
                        child: Column(
                          children: [
                            TextButton(
                              child: Text(type),
                              onPressed: (){
                                print(type);
                              },
                            ),
                            StreamBuilder(
                                stream:FirebaseFirestore.instance.collection('Widget').doc(type).snapshots(),
                                builder: (context, snapshot1) {
                                  if(!snapshot1.hasData){return Align(alignment: 
                                    Alignment.center,child: Center(
                                    child: SpinKitThreeBounce(
                                      duration: Duration(milliseconds: 5000),
                                      color: color,
                                      size: 50.0,
                                    ),
                                  ),
                                  );
                                  }
                                  var id = snapshot1.data;
                                  return Container(
                                      child: Column(
                                        children: [
                                          Text(id['name'],style: Theme.of(context).textTheme.headline5,),
                                          Row(
                                            mainAxisAlignment: MainAxisAlignment.center,
                                            children: [
                                              Card(
                                                shadowColor: shadow,
                                                elevation: 5,
                                                child: Container(
                                                  color: color1,
                                                  height: 30*SizeConfig.heightMultiplier,
                                                  width: 20*SizeConfig.heightMultiplier ,
                                                  child: Image(
                                                    image: NetworkImage(id['image']),),
                                                ),
                                              ),
                                            ],
                                          ),
                                          Container(
                                            height:  45*SizeConfig.heightMultiplier,
                                            color: Colors.yellow,
                                            child: id['Type']== 'test1'
                                                ? Test1() : (id['Type']=='test2' ? Test2() :  Align(alignment: Alignment.center,child: Center(
                                              child: SpinKitThreeBounce(
                                                duration: Duration(milliseconds: 5000),
                                                color: color,
                                                size: 50.0,
                                              ),
                                            ),
                                            ) ),
                                          )
                                        ],
                                      )
                                  );
                                }
                            ),
                            Align(
                              alignment: Alignment.bottomLeft,
                              child: GestureDetector(
                                onTap: () {
                                  Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) =>
                                              HomePage()));
                                },
                                child: Align(
                                  alignment: Alignment.bottomLeft,
                                  child: Row(
                                    children: [
                                      SizedBox(
                                        width: 7,
                                      ),
                                      Icon(
                                        Icons
                                            .arrow_back_ios_outlined,
                                        size: 3.5 *
                                            SizeConfig
                                                .heightMultiplier,
                                        color: color,
                                      ),
                                      Text(
                                        "HomePage",
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline5,
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    您需要更新获取字段的方式:

    document['image'] 
    

    到:

    document.get('image')
    

    document.data()['image']
    

    来自DocumentSnapshotPlatform documentation

    DocumentSnapshotPlatform 类

    包含从 Firestore 数据库中的文档读取的数据。 数据可以通过调用data()或者调用get()来获取 一个特定的字段。

    编辑:

    您似乎正在尝试在下面的行中使用不存在的文档 ID:

    stream:FirebaseFirestore.instance.collection('Widget').doc(type).snapshots()
    

    type 应替换为文档 ID,而不是文档中对象的 name 属性,这是 type 在下面的代码 sn-p 中所代表的(来自您的评论):

     type = document['name']; 
    

    【讨论】:

    • 在这一行中:stream:FirebaseFirestore.instance.collection('Widget').doc(type).snapshots(),,您能否将“type”替换为您数据库中的其中一个 id 并查看错误是否仍然出现?
    • type 是一个创建的变量,在 firebase 中不可用。 :字符串类型,
    • 但是我设置了变量,当我点击它会变成那个变量。
    • IconButton(icon: Icon(Icons.arrow_forward_ios_outlined), onPressed: (){ setState(() { type = document['name']; print(type); Navigator.push( context, MaterialPageRoute( builder: (context) =&gt; WidgetHome())); }); })
    • 看起来数据库中不存在类型字段。您需要使用现有字段。
    猜你喜欢
    • 2021-11-03
    • 2021-10-26
    • 2021-05-16
    • 2021-05-19
    • 1970-01-01
    • 2022-09-27
    • 2021-04-27
    • 2021-06-17
    • 1970-01-01
    相关资源
    最近更新 更多