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

Hit*_*goi 4 flutter google-cloud-firestore

在此输入图像描述我有两个按钮,如果该字段存在,则它显示一个按钮,否则它显示用户其他按钮,其中我将提供将数据添加到我的数据库的功能...我正在使用云 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: (){

              },
            );
          }
        });
  }
}
Run Code Online (Sandbox Code Playgroud)

die*_*per 5

您可以使用该方法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: (){

                  },
                );
              }
Run Code Online (Sandbox Code Playgroud)

不要忘记data首先检查您的值是否不为空。