状态不佳:无法获取 DocumentSnapshotPlatform 上不存在的字段 FirebaseFirestore flutter

时间:2021-05-27 07:28:32

标签: firebase flutter google-cloud-firestore

请帮助我是 firebase 的新手,我想得到 field Widgetid name'name in 集合名称'用户'

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

用于从另一个集合中获取数据Name '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,
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),

1 个答案:

答案 0 :(得分:0)

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

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 在下面的代码片段中代表的内容(来自您的评论) :

 type = document['name'];