GPH
GPH

Reputation: 1161

flutter form save issue

The below code works fine, I click the flat button and the alert dialog popup. Questions: 1) But if I add the form.save, it shows error

flutter: The following NoSuchMethodError was thrown while handling a gesture: flutter: The setter 'clientName=' was called on null. flutter: Receiver: null flutter: Tried calling: clientName="423"

2) How to get the text input value such us: clientName..etc. I always get null. Pleas help.

class _formState extends State<form> {

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  Booking booking;


  @override
  Widget build(BuildContext context) {

    final getCourse = widget.course;


    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        //title: Text('${widget.course.title}\n地址: ${widget.course.address}\nTEL: ${widget.course.tel}'),
        title: Text('即日訂位'),
      ),
      body: Container(
        child: Form(
          key: formKey,


          child: ListView(
            children: <Widget>[

              ListTile(
                leading: Icon(Icons.person_outline),
                title: TextFormField(
                  initialValue: "",
                  onSaved: (val) => booking.clientName = val,
                  validator: (val) => val == "" ? val : null,
                  decoration: new InputDecoration(
                    labelText: "Name",

                  ),
                ),
              ),

              //Send or Post button
              FlatButton(
                textColor: Colors.blueAccent,
                child: Text("OK"),
                color: Colors.transparent,
                onPressed: () {
                  _showFormDialog();
                  SystemChannels.textInput.invokeMethod('TextInput.hide');

                },
              )
            ],
          ),
        ),

      ),
    );
  }


  void _showFormDialog() {

    final FormState form = formKey.currentState;

    if (form.validate()) {
      //form.save();


      var alert = new AlertDialog(
        content: new Row(
          children: <Widget>[
            Text("hihi")
          ],
        ),
        actions: <Widget>[
          new FlatButton(
              onPressed: () {

                form.reset();
                Navigator.pop(context);
              },
              child: Text("ok")),
          new FlatButton(onPressed: () => Navigator.pop(context),
              child: Text("cancel"))

        ],
      );
      showDialog(context: context,
          builder: (_) {
            return alert;
          });
    }
    else {
      var alert = new AlertDialog(
        content:


        new Row(
          children: <Widget>[
            Text("error!")
          ],
        ),
        actions: <Widget>[

          new FlatButton(onPressed: () => Navigator.pop(context),
              child: Text("OK"))

        ],
      );
      showDialog(context: context,
          builder: (_) {
            return alert;
          });
    }

  }
}

Upvotes: 0

Views: 3244

Answers (1)

diegoveloper
diegoveloper

Reputation: 103381

You have to initialize your Booking object first, try this:

 Booking booking = new  Booking();

Upvotes: 1

Related Questions