ToMatt
ToMatt

Reputation: 53

Pass parameter on a onPressed: () of a form

I"m making a app who tells you the weather acording to your codal post number, buut I have a problem. I can't send the info to my function. My code is like that:

Form (
  key: _formKey,
  child: TextFormField(
    decoration: const InputDecoration(
      hintText: 'Digite seu CEP',
    ),
    keyboardType: TextInputType.number,
    inputFormatters: <TextInputFormatter>[
      FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
    ],
    validator: (value) {
      if (value.isEmpty) {
        return 'Por favor insira um CEP';
      }
      if (value.length != 8) {
        return 'Por favor digite um CEP válido';
      }
      return null;
    },
  ),
),
ElevatedButton(
  onPressed: () {
    if (_formKey.currentState.validate()) {
      getWeather(value);
    }
  },
  child: Text('Consultar'),
),

My function:

Future getWeather(String cep) async {
    print(cep);

    /*http.Response consulta = await http.get (
      "https://viacep.com.br/ws/" + cep + "/json/"
    );*/
}

In the getWeather() function I need to receive the postal code. I googled but I didn't find any awnser.

The value in the getWeather(value) give me this: "Undefined name 'value'. Try correcting the name to one that is defined, or defining the name." If I pass a fixed value like getWeather("12345678") works fine

Upvotes: 1

Views: 308

Answers (1)

BLKKKBVSIK
BLKKKBVSIK

Reputation: 3548

You actually have the right answer, you should do getWeather(value)

And in you function definition, declare the argument.

void getWeather(String zipcode) {
    print(zipcode);
}

Upvotes: 1

Related Questions