Home > Enterprise >  Provider is not updating my ListView - Singleton
Provider is not updating my ListView - Singleton

Time:02-04

I can't figure why my widget ScrollViewStory won't update when CurrentStoryModel.instance changes.

Is there a problem with the fact that CurrentStoryModel is a singleton ?

Here are the widgets:

class Story extends StatelessWidget {
  @override
  Widget build(BuildContext _) {

    return ChangeNotifierProvider<CurrentStoryModel>(
      create: (_) => CurrentStoryModel.instance,
      child: Expanded(
        // fill the whole space
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(
              color: Colors.blue,
              width: 2,
            ),
          ),
          child: ScrollViewStory(),
        ),
      ),
    );
  }
}


class ScrollViewStory extends StatefulWidget {
  ScrollViewStory({Key? key}) : super(key: key);

  @override
  State<ScrollViewStory> createState() => _ScrollViewStoryState();
}

class _ScrollViewStoryState extends State<ScrollViewStory> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return Consumer<CurrentStoryModel>(
      builder: (context, currentStoryModel, child) => ListView.builder(
          itemCount: currentStoryModel.sections.length,
          itemBuilder: (BuildContext context, int index) {
            return new StorySection(section: currentStoryModel.sections[index]);
          }),
    );
  }
}

And my model:

class CurrentStoryModel extends ChangeNotifier {

  static final CurrentStoryModel _singleton = CurrentStoryModel._();

  // Singleton accessor
  static CurrentStoryModel get instance => _singleton;

  List<SectionModel> _sections = [];

  CurrentStoryModel._();


  List<SectionModel> get sections => _sections;

  // add new entry
  void addSection(SectionModel newSection) {
    _sections.add(newSection);
    notifyListeners();
  }
}

In another part of my code, I call addSection() and the new section is effectively added to the list. But my ListView won't update...

Any help appreciated ! Thanks.

I don't really know what to do...

CodePudding user response:

It happens that for the ChangeNotifier to know that your list has changed, you should not add with .add because the list will remain the same. What you must do is to pass it a new instance. Your code would look like this:

class CurrentStoryModel extends ChangeNotifier {

  static final CurrentStoryModel _singleton = CurrentStoryModel._();

  // Singleton accessor
  static CurrentStoryModel get instance => _singleton;

  List<SectionModel> _sections = [];

  CurrentStoryModel._();


  List<SectionModel> get sections => _sections;

  // add new entry
  void addSection(SectionModel newSection) {
    _sections = [..._sections, newSection];
    notifyListeners();
  }
}

Sorry for my bad english

CodePudding user response:

In fact, the widget was updating, but new data was below the screen boundaries. All I needed to do was scrolling...

  • Related