如何检查Firebase中是否存在int字段

时间:2018-07-03 11:48:49

标签: google-cloud-firestore flutter

enter image description here我有两个按钮,如果该字段存在,则显示一个按钮,否则显示其他用户,其中我将提供向数据库添加数据的功能... im使用cloud firebase ...如何检查字段是否存在...下面是我的代码...

//如果不存在得分,它将显示一个凸起的按钮,因此您可以单击该得分以

//代码:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

BuildContext buildContext;
int filter = 0;
StreamSubscription<DocumentSnapshot> subscription;

class ScorePage extends StatelessWidget {

  final int score;
  ScorePage({Key key, @required this.score}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(
          title: new Text('Scroe Card'),
        ),
        body: new Center(
            child: new Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  new Padding(padding: EdgeInsets.all(30.0)),
                  new MaterialButton(
                    color: Colors.red,
                    onPressed: () {},
                    child: new Text(
                      "Reset Quiz",
                    ),
                  ),
                  new Flexible(
                      child: StreamBuilder(
                        stream: Firestore.instance.collection('quiz score').snapshots(),
                        builder: (BuildContext context,
                            AsyncSnapshot<QuerySnapshot> snapshot) {
                          if (!snapshot.hasData) {
                            return new Text("loading..");
                          } else {
                            return FirestoreListView1(
                              documents: snapshot.data.documents,
                            );
                          }
                        },
                      ))
                ])));
  }
}



class FirestoreListView1 extends StatefulWidget {
  final List<DocumentSnapshot> documents;
  FirestoreListView1({this.documents});
  @override
  FirestoreListView1State createState() {
    return new FirestoreListView1State();
  }
}

class FirestoreListView1State extends State<FirestoreListView1> {
  @override
  Widget build(BuildContext context1) {
    return ListView.builder(
        itemCount: widget.documents.length,
        padding: new EdgeInsets.all(1.0),
        itemBuilder: (BuildContext context1, int index) {
          int scoren = widget.documents[index].data['score1'];

          if () {
            return new RaisedButton(
              child: new Text("Submit Your Score",
                  style: new TextStyle(
                      fontSize: 20.0,
                      color: Colors.white,
                      fontFamily: "ChelaOne-Regular")),
              onPressed: (){
              },
            );
          } else {
            return new RaisedButton(
              color: Colors.red,
              child: new Text("no scores found"),
              onPressed: (){

              },
            );
          }
        });
  }
}

2 个答案:

答案 0 :(得分:1)

您可以使用方法containsKey检查文档的数据是否具有该字段。

    final bool hasScore = widget.documents[index].data.containsKey('Score1');

    if (hasScore) {
                return new RaisedButton(
                  child: new Text("Submit Your Score",
                      style: new TextStyle(
                          fontSize: 20.0,
                          color: Colors.white,
                          fontFamily: "ChelaOne-Regular")),
                  onPressed: (){
                  },
                );
              } else {
                return new RaisedButton(
                  color: Colors.red,
                  child: new Text("no scores found"),
                  onPressed: (){

                  },
                );
              }

别忘了先检查您的data是否不为空。

答案 1 :(得分:0)

itemBuilder: (BuildContext context1, int index) {
  int scoren = widget.documents[index]?.data['score1'];

      if (scoren == null) {
        return new RaisedButton(
          child: new Text("Submit Your Score",
              style: new TextStyle(
                  fontSize: 20.0,
                  color: Colors.white,
                  fontFamily: "ChelaOne-Regular")),
          onPressed: (){
          },
        );
      } else {
        return new RaisedButton(
          color: Colors.red,
          child: new Text("no scores found"),
          onPressed: (){

          },
        );
      }
}

检查为scoren == null,因为在您的文档中没有分数时,地图获取程序将返回null

我还将 null-safe 运算符添加到您的文档获取器:documents[index]?..(问号)。这样可以确保将null返回到scoren,而不是尝试使用map getter并引发异常。