Nicolas
Nicolas

Reputation: 13

Flutter TextFormField validator

I have a problem, someone knows how to put an error message from validator on an AlertDialog or a pop window with an 'ok' button to dismiss the pop window. This error has return =>

The return type 'AlertDialog' isn't a 'String', as defined by anonymous closure.

                              Align(
                                  alignment: Alignment.centerLeft,
                                  child: Padding(
                                    padding: EdgeInsets.fromLTRB(18, 22, 0, 4),
                                    child: Text(
                                      "Code Postal",
                                      style: TextStyle(
                                          color: Colors.white, fontSize: 16),
                                    ),
                                  )),
                              Align(
                                  alignment: Alignment.centerLeft,
                                  child: Container(
                                    height:
                                        MediaQuery.of(context).size.height / 13,
                                    width:
                                        MediaQuery.of(context).size.width / 1.5,
                                    decoration: BoxDecoration(
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(10.0))),
                                    padding: EdgeInsets.fromLTRB(18, 0, 18, 0),
                                    child: TextFormField(
                                      controller: codePostalController,
                                      onChanged: (value) {
                                        setState(() {
                                          codePostal = value;
                                        });
                                      },
                                      validator: (value) => value.length != 5
                                          ? AlertDialog(content: Text('Postal Code must be five digits.'))
                                          : null,
                                      keyboardType: TextInputType.number,
                                      decoration: InputDecoration(
                                        contentPadding: EdgeInsets.symmetric(
                                            vertical: 0, horizontal: 10),
                                        hintStyle: TextStyle(
                                            color: Color.fromRGBO(
                                                133, 133, 133, 1.0),
                                            fontSize: 16),
                                        border: OutlineInputBorder(
                                          borderSide: BorderSide.none,
                                          borderRadius:
                                              BorderRadius.circular(10.0),
                                        ),
                                        suffixIcon: Icon(Icons.search,
                                            color: Color.fromRGBO(
                                                133, 133, 133, 1.0)),
                                        hintText: 'Code postal',
                                        fillColor:
                                            Color.fromRGBO(40, 40, 40, 1.0),
                                        filled: true,
                                      ),
                                    ),
                                  )),

Upvotes: 0

Views: 1051

Answers (1)

CopsOnRoad
CopsOnRoad

Reputation: 268524

That's because you're returning a Dialog when you are supposed to return a String.

Replace this

validator: (value) => value.length != 5
  ? AlertDialog(content: Text('Postal Code must be five digits.'))
  : null,

with this

validator: (value) => value.length != 5
  ? 'Postal Code must be five digits.'
  : null,

And if you want to show AlertDialog, use showDialog() method in validator like:

validator: (value) {
  if (value.length != 5) {
    showDialog(context: context, builder: (_) => AlertDialog(title: Text("Error")));
  }
  return null;
}

Upvotes: 1

Related Questions