'data change in Provider variable but widget can't show data and return nothing

enter image description here i have an array in my Provider class like this

  List<String> _modelImages = [];
  List<String> get modelImages {
    return [..._modelImages];
  }

and i have a function that add data to array

 void addModel(dynamic modelSelected) async {
    try {
      var isExist = _modelImages.contains(modelSelected["id"]);
      if (!isExist) {
        _modelImages.add(modelSelected["id"]);
        print(_modelImages);
      }
      notifyListeners();
    } catch (error) {
      print(error);
    }
  }

when i print array in up function show me that String added to array but i have other funciton like below

  void showMe() {
    print(_modelImages);
  }

my problem is when i want to print _modelImages array in ShowMe function show me empty array

what is the problem?



Solution 1:[1]

the problem was for set changeNotifierProvider in on up level but is use in higher level multi provider in main.dart file i removed changeNotifierProvider in one up level problem solved thanks guys

when face with these problems like this just go one up level and remove ChangeNotifierProvider for example

body: ChangeNotifierProvider(
          create: (ctx) => YOUR_PROVIDER_CLASS_NAME(),
          child: Container(
            child: YOUR_WIDGER(
          ),
        ));

and leave Container alone for this example but you should use multi provider in highest level before

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