'How update provider's data in Flutter Dart

I have a list of a few things and each has its own ID. I want to pass that certain ID to the provider whenever it's clicked. But as of now, when I click on ITEM ONE in the list for the first time, the ID of ITEM ONE is passed to the provider, but when I click again on a different item in the list the ID of that item is not updated in the provider. I want to know, how can I update the data of the provider every time a new item is clicked.

here are my codes:

  • Passing Data
onTap: () async {
  Provider.of<providerFile>(context, listen: false).setdataID(DataID);
  Navigator.push(context, MaterialPageRoute(builder: (context) => NextPageHome(DataID: DataID)));
},


  • Provider File
String DataID = '';

  void setdataID(String pin) {
    if (DataID.isEmpty) {
      DataID = pin;
      notifyListeners();
    }
  }

Thanks, programmers.



Solution 1:[1]

At second time DataID is not anymore empty and therefore not set.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 roosi