Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When adding an item to an ObservableList and it is observed, the index is length of the list #193

MaikuB opened this issue Jun 9, 2019 · 3 comments


Copy link

@MaikuB MaikuB commented Jun 9, 2019

I've defined a store as follows

import 'package:mobx/mobx.dart';
import '../models/item.dart';

part 'data_store.g.dart';

class DataStore = _DataStore with _$DataStore;

abstract class _DataStore with Store {
  ObservableList<Item> items = ObservableList<Item>();

  void addItem() {

In one of my widgets, I observe if the list has been modified

  void initState() {
    super.initState(); {
      if (listChange.added?.isNotEmpty ?? false) {
        <handle when item has been added>

When an item has been added via the addItem method within the store, the callback is invoked in the widget to notify me that an item has been added. Upon inspect the value of listChange.index, it appears that rather than being the index of where the item has been added, it's actually the length of the list. For example, if this is the first item that's been added, I'm expecting listChange.index to have a value of 0, instead the actual value is 1

Provided it's an actual bug (which I suspect is the case), this appears to be where the fix needs to be applied

Edit: code snippets are taken from this repo if you need to be able to reproduce the issue. I can look at submitting a PR as well though should be an easy fix

Copy link

@pavanpodila pavanpodila commented Jun 9, 2019

Hey @MaikuB, that is indeed a bug 🥇. I'll be more than happy to take in a PR.

I think that way you also get a taste of how MobX works internally and get a chance to explore other collection types: ObservableMap, ObservableSet and of course ObservableList to see if similar issues exist over there. Want to take a crack at it ?

Copy link
Collaborator Author

@MaikuB MaikuB commented Jun 9, 2019

Sure. Won't be till much later that I can take a look though. Getting late in Australia so struggling to stay up to watch the cricket and code at the same time ;)

Copy link

@pavanpodila pavanpodila commented Jun 9, 2019

Ha ha, same in "Watching Cricket". Take rest and get back when you can. Great ideas might come in the sleep 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants