I am working on Flutter, Calling API based on Search Parameters.
What I have done is:
- Search Box created using TextField
- onChange of that TextField, I have called method to get data from server.
- ListView displaying a list
Problem: When I hide the keyboard after my searching is done using back press, It's requesting again to get data.
You can check video for better explanation:
Here is my code:
getTextInputField() {
return Flexible(
fit: FlexFit.tight,
flex: 1,
child: TextFormField(
controller: _text,
decoration: new InputDecoration(
labelText: "Enter Area for Pincode",
fillColor: Colors.white,
errorText: _validateAgain ? 'This should not be empty!' : null,
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(15.0),
borderSide: new BorderSide(),
),
//fillColor: Colors.green
),
validator: (val) {
if (val.length == 0) {
return "This shouldn't be empty!";
} else {
return null;
}
},
onChanged: (value) {
getData();
},
keyboardType: TextInputType.streetAddress,
),
);
}
getData() {
setState(() {
if (_text.text.isNotEmpty) {
if (_text.text.length > 2) {
_validateAgain = false;
textValue = _text.text;
_apiCall = true;
_callAPIForPinCode();
}
} else
_validateAgain = true;
});
}
How I prevent calling API again when I hide the keyboard using back press.
Note: If I hide the keyboard using the done key from the keyboard, it's not calling API and hiding smoothly.
Edited:
As I have tested offline right now, It's calling offline too. It's calling the event of onChange on dismissing the keyboard.