altafc22 / csc_picker

A flutter package to display a country, states, and cities. In addition it gives the possibility to select a list of countries, States and Cities depends on Selected, also you can search country, state, and city all around the world.
BSD 3-Clause "New" or "Revised" License
27 stars 111 forks source link

CSCPickerState._onSelectedCountry #52

Open ghost opened 2 years ago

ghost commented 2 years ago

E/flutter (20371): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value E/flutter (20371): #0 CSCPickerState._onSelectedCountry. (package:csc_picker/csc_picker.dart:741:35) E/flutter (20371): #1 State.setState (package:flutter/src/widgets/framework.dart:1109:30) E/flutter (20371): #2 CSCPickerState._onSelectedCountry (package:csc_picker/csc_picker.dart:723:5) E/flutter (20371): #3 CSCPickerState._setDefaultCountry (package:csc_picker/csc_picker.dart:618:7) E/flutter (20371): #4 CSCPickerState.getCountries (package:csc_picker/csc_picker.dart:647:5) E/flutter (20371): E/flutter (20371): carbon carbon (1)

salvaterra commented 2 years ago

Try this

///triggers once country selected in dropdown
                    onCountryChanged: (value) {
                      setState(() {
                        ///store value in country variable
                        countryValue = value;
                      });
                    },

                    ///triggers once state selected in dropdown
                    onStateChanged: (value) {
                      setState(() {
                        ///store value in state variable
                        stateValue = value ?? "";
                      });
                    },

                    ///triggers once city selected in dropdown
                    onCityChanged: (value) {
                      setState(() {
                        ///store value in city variable
                        cityValue = value ?? "";
                      });
ghost commented 2 years ago

still makeing problem if you can edit my code will be fine

Heydran commented 1 year ago

I had a similar error that occurred because when you change the value of country and state, the callbacks of its children state and city or just city in the case of the state, are called passing the NULL value as a parameter, this occurs because the state is /or city is reset to not have invalid combinations.

To prevent the error you just need to create the 3 onchange callbacks, for country, city and state keeping in mind that they will be called passing the NULL parameter.

sorry, translator inglish. I am Brazilian.