简体   繁体   中英

Flutter Form Builder and firestore

I am working with the Flutter_form_builder and radio buttons.

I current have the text fields that are working properly using their controllers and then writing to firebase firestore. When I try to assign the Radio Button variable to write to firebase, it writes both yes and no even through it is printing the answer that is selected in the simulator or phone in the console

How do you convert this to a variable that uploads the correct answer?

Top of file

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();
 }

The actual widget

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'),
                      ),
                    ],
                  ),

The Submit button

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();
                        },
                      ),
                    ],
                  ),

Ok,

I am apparently and schmuck

Here is the fixed code.

I just had to declare the key to add to the current document rather than trying to make each one its own controller and variable.

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();
                        },
                      ),
                    ],
                  ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM