Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can check int field exist or not in firebase

enter image description hereI have two button where if the field is exist then its shows one button otherwise its shows user other in which i will give function to add data to my database ...i m using cloud firebase ...how can check the field is exist or not ...below is my code...

//if the score is not existing it will show a raised button so u can click on that to

//code:

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: (){

              },
            );
          }
        });
  }
}
like image 827
Hitanshu Gogoi Avatar asked Nov 16 '25 05:11

Hitanshu Gogoi


1 Answers

You can use the method containsKey to check if your Document's data have that field.

    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: (){

                  },
                );
              }

Don't forget to check if your data is not null first.

like image 144
diegoveloper Avatar answered Nov 17 '25 20:11

diegoveloper