-
Notifications
You must be signed in to change notification settings - Fork 4
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
RangeError (index): Invalid value: Not in inclusive range #8
Comments
I elaborated here how diffutil works. The updates are relative to the old list.
Currently, there is no direct way to get the item that was inserted or removed. You can check out the branch |
I have a similar issue to this one, so I think the comment is relevant. I've prepared a few tests, as I wanted to make sure if my code is correct or not. In those tests, the last 4 do not pass. test('should insert last two items of new list', () {
final newList = [1, 2, 3, 4];
final oldList = [1, 2];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [3, 4]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);
});
test('should insert first two items of new list', () {
final newList = [1, 2, 3, 4];
final oldList = [3, 4];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [1, 2]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);});
test('should insert last item of new list', () {
final newList = [1, 2, 3, 4];
final oldList = [1, 2, 3];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [4]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);});
test('should insert first item of new list', () {
final newList = [1, 2, 3, 4];
final oldList = [2, 3, 4];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [1]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);});
test('should insert all new items from new list', () {
final newList = [1, 2, 3, 4];
final oldList = <int>[];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [1, 2, 3, 4]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);});
test('should insert new item from middle of new list', () {
final newList = [1, 2, 3, 4];
final oldList = [1, 2, 4];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [3]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);});
test('should remove last two items', (){
final newList = [1, 2 ];
final oldList = [1, 2, 3, 4];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, []);
expect(diff.toUpdate, []);
expect(diff.toRemove, [3, 4]);
});
test('should remove last item', (){
final newList = [1, 2, 3];
final oldList = [1, 2, 3, 4];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, []);
expect(diff.toUpdate, []);
expect(diff.toRemove, [4]);
});
test('should remove first two items', (){
final newList = [3, 4];
final oldList = [1, 2, 3, 4];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, []);
expect(diff.toUpdate, []);
expect(diff.toRemove, [1, 2]);
});
test('should remove first item', (){
final newList = [2, 3, 4];
final oldList = [1, 2, 3, 4];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, []);
expect(diff.toUpdate, []);
expect(diff.toRemove, [1]);
});
test('should remove middle item', (){
final newList = [1, 2, 4];
final oldList = [1, 2, 3, 4];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, []);
expect(diff.toUpdate, []);
expect(diff.toRemove, [3]);
});
test('should insert in the middle and one at the end', (){
final newList = [1, 2, 3, 4];
final oldList = [1, 3];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [2, 4]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);
});
test('should insert in the middle and two at the end', (){
final newList = [1, 2, 3, 4, 5];
final oldList = [1, 3];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [2, 4, 5]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);
});
test('should insert in the middle and one at the start', (){
final newList = [1, 2, 3, 4];
final oldList = [2, 4];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [1, 3]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);
});
test('should insert int the middle and two at the start', (){
final newList = [1, 2, 3, 4, 5];
final oldList = [3, 5];
final diff = obtainDiff(
delegate: _IntDiffDelegate(oldList, newList),
newList: newList,
oldList: oldList,
);
expect(diff.toInsert, [1, 2, 4]);
expect(diff.toUpdate, []);
expect(diff.toRemove, []);
}); where DiffResult<T> obtainDiff<T>({
@required DiffDelegate delegate,
@required List<T> newList,
@required List<T> oldList,
bool detectMoves = true,
}) {
final diff = calculateDiff(
delegate,
detectMoves: detectMoves,
);
final toInsert = <T>[];
final toUpdate = <T>[];
final toRemove = <T>[];
for (var update in diff.getUpdates(batch: true)) {
update.when(
insert: (pos, count) {
toInsert.addAll(newList.sublist(pos, pos + count));
},
change: (pos, payload) {
toUpdate.add(payload);
},
remove: (pos, count) {
toRemove.addAll(
oldList.sublist(pos, (pos + count).clamp(pos + 1, oldList.length)));
},
move: (from, to) {
print("from: $from, to: $to");
},
);
}
return DiffResult(toInsert, toUpdate, toRemove);
} I've observed that in those 4 tests the insert method gives wrong values as it says "from position 1, two items should be inserted" instead of "from position 1 insert one item and from position 3 insert one item", so somehow it skips the item in the middle. In my current project, I am not on Flutter 2.0 so I cannot check the branch with proof of concept, maybe later this week I will do it. I think those tests might be helpful with further development. |
I see. Yeah, with the current API, it is very hard to actually get the concrete items that have been removed or inserted. If you provide a minimal executable example I could look further into this, but I think that the result is correct, but diffutil just does not do what you need at the moment. What you need are is possible with experimental changes I made in the branch Note that the implementation is pretty hacky at the moment (the changes only work with implementations of If I were to actually release this feature, I would probably provide separate methods for users that need this to avoid breaking changes. But if you are able to check out this branch, I am happy to hear your feedback. |
I implemented this in a cleaner way and merged it on master. On how to use it, see https://github.com/knaeckeKami/diffutil.dart#updates-with-data |
Help please. When i try to use a simple solution i get error. Maybe I am using diffUtil incorrectly somehow?...
My code:
Error i get:
What i expected:
The text was updated successfully, but these errors were encountered: