-1

我正在使用 Flutter_form_builder 和单选按钮。

我目前的文本字段使用他们的控制器正常工作,然后写入 firebase firestore。当我尝试将 Radio Button 变量分配为写入 firebase 时,它​​会同时写入 yes 和 no,即使它正在打印模拟器中选择的答案或控制台中的电话

您如何将其转换为上传正确答案的变量?

文件顶部

class _FFPServicesApplicationWidgetState extends State<FFPServicesApplicationWidget> {
 var data;
 bool autoValidate = true;
 bool readOnly = false;
 bool showSegmentedControl = true;
 final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();


 ValueChanged _onReferral =(val) => print(val);
 var referralOptions = ['Yes', 'No',];

 get _fbUserKey => null;


 void onItemTwoPressed(BuildContext context) => Navigator.push(context, 
 MaterialPageRoute(builder: (context) => HelpWidget()));



TextEditingController taskNameInputController;
TextEditingController taskEmailInputController;
TextEditingController taskPhoneInputController;
TextEditingController taskDescriptionInputController;
TextEditingController taskAgencyInputController;


@override
  initState() {
    taskNameInputController = new TextEditingController();
    taskEmailInputController = new TextEditingController();
    taskPhoneInputController = new TextEditingController();
    taskDescriptionInputController = new TextEditingController();
    taskAgencyInputController = new TextEditingController();


    super.initState();
 }

实际的小部件

FormBuilderRadio(
                    decoration: InputDecoration(labelText: 'Were you referred to us?'),
                    attribute: "Were you referred to us?",
                    onChanged: _onReferral,
                    validators: [FormBuilderValidators.required()],
                    options: [
                      FormBuilderFieldOption(
                        value: "Yes",
                        child: Text('Yes'),
                      ),
                      FormBuilderFieldOption(
                        value: "No",
                        child: Text('No'),
                      ),
                    ],
                  ),

提交按钮

Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      MaterialButton(

                        child: Text("Submit"),
                        onPressed: () {
                        if (_fbKey.currentState.saveAndValidate()) {
                        print(_fbKey.currentState.value);
                        Firestore.instance
                        .collection('email')
                        .add({
                          "1. Name": taskNameInputController.text,
                          "2. Email": taskEmailInputController.text,
                          "3. Phone Number": taskPhoneInputController.text,
                          "4. Description": taskDescriptionInputController.text,
                          "5. Were you Referred": referralOptions,
                          "6. Agency Referring?": taskAgencyInputController.text,
                          "7. Children under 18": childrenOptions,
                          "8. Lincoln County?": countyOptions,
                          "9. Received Support in the past?": supportOptions,
                        });
                          }
                        },
                      ),
                      MaterialButton(
                        child: Text("Reset"),
                        onPressed: () {
                          _fbKey.currentState.reset();
                        },
                      ),
                    ],
                  ),
4

1 回答 1

0

好的,

我显然是笨蛋

这是固定代码。

我只需要声明要添加到当前文档的键,而不是试图让每个键都有自己的控制器和变量。

Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      MaterialButton(

                        child: Text("Submit"),
                        onPressed: () {
                        if (_fbKey.currentState.saveAndValidate()) {
                        print(_fbKey.currentState.value);
                        Firestore.instance
                        .collection('email')
                        .add(_fbKey.currentState.value);
                          }
                        },
                      ),
                      MaterialButton(
                        child: Text("Reset"),
                        onPressed: () {
                          _fbKey.currentState.reset();
                        },
                      ),
                    ],
                  ),
于 2020-05-26T18:48:25.600 回答