Firebase 返回文档字段为 null

时间:2021-03-19 11:52:14

标签: firebase flutter google-cloud-firestore

我正在尝试在我的 flutter 应用程序上显示文档中的字符串,但它返回 null,其他字段显示正确,在 firestore 集合中,文档的字段存在并且它的类型为“字符串”, 所有其他字段都返回它们的值并显示出来, 即使在控制台中打印“描述”字段也是空的。

这就是我创建“描述”字段的方式

TextFormField(
              autovalidateMode: AutovalidateMode.onUserInteraction,
              keyboardType: TextInputType.multiline,
              maxLength: 30,
              onChanged: (val) {
                setState(() => description = val);
              },
              validator: (val) {
                if(val.isEmpty){
                  return 'Please Describe your Handly';
                }
                return null;
              },
              decoration: InputDecoration(
                hintText: 'Enter Description',
                hintStyle: TextStyle(
                  color: Colors.white70.withOpacity(0.5),
                ),
              ),
              style: TextStyle(
                color: Colors.white70,
                fontSize: 15.0,
              ),
            ), //description

火基法

onPressed: () async {
                if(formKey.currentState.validate()) {
                  setState(() => loading = true);
                  await HandlyCallsDatabaseService(uid: (_ath.currentUser
                      .uid + ' time ' + (DateTime
                      .now()
                      .millisecondsSinceEpoch).toString()))
                      .createHandlyCall(HandlyCall(
                    title: title,
                    type: type,
                    description: description,
                    reward: reward,
                    money: money,
                    name: _ath.currentUser.displayName,
                    score: score,
                    user: _ath.currentUser.uid,
                    time: DateTime.now().toString(),
                  ));
                  Navigator.pop(context);
                 } else {
                  print(Error);
                }
                },

CreateHandlyCall 服务

Future createHandlyCall(HandlyCall call) async {
return await handlyCallsCollection.doc(uid).set({
  'title': call.title,
  'type': call.type,
  'description': call.description,
  'reward': call.reward,
  'money': call.money,
  'name': call.name,
  'rating': call.score,
  'user': call.user,
  'time': call.time,
});
}

我尝试显示字符串的颤振代码

Text(
              'The job: ${handlyCall.description}',
               style: TextStyle(
               fontSize: 13,
               fontWeight: FontWeight.bold,
             ),
     ),

The Firestore document

The Tile in my app

3 个答案:

答案 0 :(得分:0)

在你的 Text Widget 中用这个 "" 代替 ' ' 改变你的逗号,我不明白 %100 但描述已经保存为 String ,所以你不需要在 ${} this 中写入。在对象的末尾添加 toString() 以确保。

Text(
          "The job: " + handlyCall.description.toString(),
           style: TextStyle(
           fontSize: 13,
           fontWeight: FontWeight.bold,
         ),
 ),

答案 1 :(得分:0)

我认为问题在于您在此处将空 handlyCall 传递给 HandlyCallTile

class HandlyCallTile extends StatelessWidget {
  final HandlyCall handlyCall;

  HandlyCallTile({this.handlyCall});

尝试验证您正在传递的内容,并在使用之前验证handlyCall它有数据

答案 2 :(得分:0)

好的,所以我修复了它,再次检查了我的代码,在服务文件中我遗漏了描述方法,因此当我使用该方法从我的流正在侦听的流中获取数据时,它没有被映射到列表列出类型handlyCalls 描述和时间未收到

新代码是:

 //handly calls from snapshot
List<HandlyCall> _handlyCallListFromSnapshot(QuerySnapshot snapshot) {
  return snapshot.docs.map((document) {
    return HandlyCall(
      title: document.data()['title'] ?? '',
      type: document.data()['type'] ?? '',
      reward: document.data()['reward'] ?? null,
      money: document.data()['money'] ?? 0,
      name: document.data()['name'] ?? '',
      score: document.data()['score'] ?? 25,
      user: document.data()['user'] ?? null,
      description: document.data()['description'] ?? 'No description',
      time: document.data()['time'] ?? null,
    );
  }).toList() ;
}


//get userProfile stream
Stream<List<HandlyCall>> get handlyCalls {
  return handlyCallsCollection.snapshots().
  map(_handlyCallListFromSnapshot);
}

因为这两条线直到现在才出现

      description: document.data()['description'] ?? 'No description',
      time: document.data()['time'] ?? null,
相关问题