Manal
Manal

Reputation: 189

Store multiple Checkbox values in Firestore in Flutter

I develop an applicaion for elderly, when they sing up, the app requires from them to select which chronic disease they have from multiple Checkbox that in DropDown. then I want to store these multiple choices in Cloud Firestore, I need to know how to stroe it? my code is:

import 'package:flutter/material.dart';
import 'Constants.dart';
import 'checkBox.dart';
class ChronicDiseaseDropDown extends StatelessWidget {
  Color borderColor;
  Color hintColor;
  Color iconColor;
  ChronicDiseaseDropDown({
    this.borderColor = white,
    this.hintColor = white,
    this.iconColor = white,
  });
  @override

  //List<Map<String, String>> chronicDisease= [{'id':'1', 'disease':'أمراض القلب'},];

  final chronicDiseaseList = const [
    {'id': 1, 'disease': 'أمراض القلب'},
    {'id': 2, 'disease': 'أمراض السكري'},
    {'id': 3, 'disease': 'أمراض الجهاز التنفسي'},
    {'id': 4, 'disease': 'أمراض السرطان'},
    {'id': 5, 'disease': 'أمراض ارتفاع ضغط الدم'},
  ];

  bool isHeartDisease = false;
  bool isDiabetes = false;
  bool isRespiratorySystemDisease = false;
  bool isCancer = false;
  bool isHighBloodDisease = false;



  String dropdownValue = 'First';
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 50,
      width: 350,
      child: DropdownButtonFormField(
        iconSize: 50,
        iconEnabledColor: iconColor,
        decoration: InputDecoration(
          hintText: 'الأمراض المزمنة',
          hintStyle: TextStyle(
              fontSize: 23, fontWeight: FontWeight.bold, color: hintColor),
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: borderColor,
            ),
            borderRadius: BorderRadius.circular(30.0),
          ),
        ),
        items: [
          DropdownMenuItem(
            child: Row(
              children: <Widget>[
                CheckboxSelectorPage(isHeartDisease),
                Text(
                  chronicDiseaseList[0]['disease'],
                  style: textStyle1,
                ),
              ],
            ),
          ),
          DropdownMenuItem(
            child: Row(
              children: <Widget>[
                CheckboxSelectorPage(isDiabetes),
                Text(
                  chronicDiseaseList[1]['disease'],
                  style: textStyle1,
                ),
              ],
            ),
          ),
          DropdownMenuItem(
            child: Row(
              children: <Widget>[
                CheckboxSelectorPage(isRespiratorySystemDisease),
                Text(
                  chronicDiseaseList[2]['disease'],
                  style: textStyle1,
                ),
              ],
            ),
          ),
          DropdownMenuItem(
            child: Row(
              children: <Widget>[
                CheckboxSelectorPage(isCancer),
                Text(
                  chronicDiseaseList[3]['disease'],
                  style: textStyle1,
                ),
              ],
            ),
          ),
          DropdownMenuItem(
            child: Row(
              children: <Widget>[
                CheckboxSelectorPage(isHighBloodDisease),
                Text(
                  chronicDiseaseList[4]['disease'],
                  style: textStyle1,
                ),
              ],
            ),
          )
        ].toList(),
        onChanged: (value) {},
      ),
    );
  }
}

I want to store checkbox values in field ChronicDisease: enter image description here

Editied part:

CheckboxSelectorPage class code

class CheckboxSelectorPage extends StatefulWidget {
  bool isChecked = false;

  CheckboxSelectorPage(this.isChecked, {Key key}) : super(key: key);

  @override
  _CheckboxSelectorPageState createState() => _CheckboxSelectorPageState();
}

class _CheckboxSelectorPageState extends State<CheckboxSelectorPage> {
  @override
  Widget build(BuildContext context) {
    return Checkbox(
      onChanged: (bool value) {
        widget.isChecked = value;
        setState(() {
          widget.isChecked = value;
        });
      },
      value: widget.isChecked,
    );
  }
}

Upvotes: 0

Views: 1564

Answers (1)

Joeman
Joeman

Reputation: 251

Are you saying you want an array of booleans. If so it would look something like this.

yourDocRef.update({
    ChronicDiseases: firebase.firestore.FieldValue.arrayUnion(<your booleans here>)
});

to get the document ref you will have to make a query because you don't have a predictable doc id but the code above will add to that array once you create a reference to the specific doc. Maybe make the document id the user's id for easier look up. Not sure if you have checked this post but this is your answer here.

Upvotes: 2

Related Questions