Firebase中的Flutter复选框值存储

n53p2ov0  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(100)

我的设置很简单:我正在使用Flutter web制作一个登录页面:用户输入他们的电子邮件地址,并勾选复选框,如果他们说他们超过18岁。对于我的生活,我无法找到一种方法来存储电子邮件地址和复选框的布尔值在Firebase中的同一个记录-UI和Firebase都设置和工作正常,这里的代码片段:

`child: TextFormField(
                      controller: _emailController,
                      // The validator receives the text that the user has entered.
                      validator: (val) => !EmailValidator.validate(val!, true)
                          ? 'Please enter a valid email.'
                          : null,
                      onSaved: (email2save) => this.email2save = email2save,
                      decoration: InputDecoration(
                          icon: Icon(
                            Icons.email_outlined,
                            color: Color(0xFF0000CC),
                          ),
                          hintText: "Please enter your email",
                          border: InputBorder.none),
                    ),
.......
Column(
                    children: [
                      Text('Please tick here'),
                      MyStatefulWidget(),
                      Text(' if over 18'),
                    ],
                  ),
.......
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  bool isChecked = false;

  @override
  Widget build(BuildContext context) {
    Color getColor(Set<MaterialState> states) {
      const Set<MaterialState> interactiveStates = <MaterialState>{
        MaterialState.pressed,
        MaterialState.hovered,
        MaterialState.focused,
      };
      if (states.any(interactiveStates.contains)) {
        return Color(0xFF0000CC);
      }
      return Colors.black;
    }

    return Checkbox(
      checkColor: Colors.white,
      fillColor: MaterialStateProperty.resolveWith(getColor),
      value: isChecked,
      onChanged: (bool? value) {
        setState(() {
          isChecked = value!;
        });
      },
    );
  }
yyyllmsg

yyyllmsg1#

嗨,我没有看到任何Firebase在代码中,你已经提供了以上,但它实际上是很容易添加两个甚至许多东西一次到云Firestore:

var collection = FirebaseFirestore.instance
            .collection(collection_name)
            .doc(document_name);
 collection
                    .set({
                      'email': 'email2save',
                      'check': 'isChecked'
                    })

你可以在课堂上的任何地方做这个,它会像魔术一样起作用。

相关问题