0

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

coldasspirit
  • 127
  • 1
  • 11
  • This question needs elaboration. What code is updating Firestore? Are you using `ChangeNotifierProvider`? (It needs to wrap what you you want changed -- or are you expecting to update Firestore and then refresh from a stream subscription?) – AWhitford Dec 21 '20 at 22:48
  • The `DropdownButton` has a hard-coded value: `value: constantValue,` -- but that needs to be the value from `PostProvider`, like: `context.watch().postCategory`. – AWhitford Dec 24 '20 at 09:04

1 Answers1

1

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.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70