Search code examples
flutterdartdrop-down-menuproviderflutter-state

DropdownButton updating with Provider


When i choose item in the below list, it does not change. If i click the others,then nothing happens. How can i change this value both firestore and UI ? I know, i need to update value: constantValue, this code, but how i can do that with provider?

Here, button:

Widget dropdownButton(BuildContext context) {
String constantValue = "League Of Legends";
return DropdownButton(
    value: constantValue,
    onChanged: (newValue) {
      context.read<PostProvider>().postCategory = newValue;
    },
    items: <String>["League Of Legends", "Steam", "Csgo"]
        .map<DropdownMenuItem<String>>((String value) {
      return DropdownMenuItem<String>(
        value: value,
        child: Text(value),
      );
    }).toList());

}

and also provider:

     String _postCategory;

     String get postCategory => _postCategory;

  set postCategory(String value) {
    _postCategory = value;
    notifyListeners();
  }

enter image description here


Solution

  • You need to include something to write FireBase in the code path. Provider doesn't magically do that for you. However, you can have various providers or widgets wake up on that notify_listeners() call.