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

Closed
MaikuB opened this issue Jun 9, 2019 · 3 comments

Comments

Projects
None yet
2 participants
@MaikuB
Copy link
Contributor

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 {
  @observable
  ObservableList<Item> items = ObservableList<Item>();

  @action
  void addItem() {
    items.add(Item(DateTime.now().toUtc()));
  }
}

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

@override
  void initState() {
    super.initState();
    widget.store.items.observe((listChange) {
      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 https://github.com/MaikuB/flutter_mobx_animatedlist_demo 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

@pavanpodila

This comment has been minimized.

Copy link
Member

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 ?

@MaikuB

This comment has been minimized.

Copy link
Contributor Author

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 ;)

@pavanpodila

This comment has been minimized.

Copy link
Member

commented Jun 9, 2019

Ha ha, same here...as 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
You can’t perform that action at this time.