Shrutimahajan / Google-AutoComplete-TextField-Flutter

MIT License
28 stars 98 forks source link

The argument type 'void Function(Prediction)' can't be assigned to the parameter type 'void Function(Prediction)?' #20

Open 10terabyte opened 1 year ago

10terabyte commented 1 year ago
GooglePlaceAutoCompleteTextField(
                textEditingController: controller,
                googleAPIKey: googleApikey,
                inputDecoration: InputDecoration(),
                debounceTime: 800, // default 600 ms,
                countries: ["ng"],// optional by default null is set
                isLatLngRequired: true,// if you required coordinates from place detail
                getPlaceDetailWithLatLng: (Prediction prediction) {
                  print("placeDetails" + prediction.lng.toString());
                }, // this callback is called when isLatLngRequired is true
                itmClick: (Prediction prediction) {
                  controller.text=prediction.description;
                  controller.selection = TextSelection.fromPosition(TextPosition(offset: prediction.description.length));
                }
            )

I get error from this code. Just used this code from sample. Please let me know what is the issue.

srinivas-nahak commented 1 year ago

It seems like there might be a type mismatch in the argument you're passing to the itmClick callback function. The error message is indicating that the expected type for the argument is void Function(Prediction)?, which means it can accept either a function or null. However, it appears that you're passing a non-nullable void Function(Prediction).

To fix this issue, you can make the itmClick callback function nullable by adding a question mark (?) after the function type. This modification will allow the argument to accept both functions and null values.

This change should resolve the type mismatch problem and ensure that the itmClick callback function behaves as intended.

itmClick: (Prediction? prediction) {
  if (prediction != null) {
    controller.text = prediction.description;
    controller.selection = TextSelection.fromPosition(TextPosition(offset: prediction.description.length));
  }
}