Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

ReplayCubit - List with mutated state can not be redo / undo #60

Closed
md-weber opened this issue Jul 6, 2020 · 11 comments
Closed

ReplayCubit - List with mutated state can not be redo / undo #60

md-weber opened this issue Jul 6, 2020 · 11 comments
Assignees
Labels
question Further information is requested
Projects

Comments

@md-weber
Copy link

md-weber commented Jul 6, 2020

Describe the bug
If I execute redo/undo on a state that has been spread into a new list it seems that I cannot redo/undo the state.

To Reproduce
Create a cubit with a List of elements and change the values of the list. After that spread, the list into the emit of the cubit. The CubitBuilder gets retriggered but with the old state.

  void selectDrink(Drink drink, bool selected) {
    state.firstWhere((element) => element.name == drink.name).selected =
        selected;
    emit([...state]);
  }

Expected behavior
The redo and undo function should work properly and update the state for the List.

**Logs **

Analyzing state_tutorials...                                            
No issues found! (ran in 2.8s)
[✓] Flutter (Channel stable, v1.17.4, on Mac OS X 10.15.5 19F101, locale en-DE)
    • Flutter version 1.17.4 at /Users/myracle/tools/flutter
    • Framework revision 1ad9baa8b9 (3 weeks ago), 2020-06-17 14:41:16 -0700
    • Engine revision ee76268252
    • Dart version 2.8.4

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
    • Android SDK at /Users/myracle/Library/Android/sdk
    • Platform android-30, build-tools 30.0.0
    • Java binary at: /Users/myracle/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6514223/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • CocoaPods version 1.9.1

[✓] Android Studio (version 4.0)
    • Android Studio at /Users/myracle/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6514223/Android Studio.app/Contents
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.46.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.12.1

[✓] Connected device (1 available)
    • sdk gphone x86 • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)

• No issues found!
@felangel
Copy link
Owner

felangel commented Jul 6, 2020

Hi @md-weber 👋
Thanks for opening an issue!

Are you able to provide a link to a sample app which I can run to reproduce the issue? Thanks 🙏

@felangel felangel self-assigned this Jul 6, 2020
@felangel felangel added question Further information is requested waiting for response Waiting for follow up labels Jul 6, 2020
@felangel felangel added this to To do in cubit via automation Jul 6, 2020
@pedromassango
Copy link

Maybe this is related, so I'm going to add it here and let me know if I need to open a new issue.

Cubit is not saving the state after emit(). If I get something please let me know: In the following example I expect the last print to print "State: 3" but it prints: "State: 0".

import 'package:cubit/cubit.dart';

void main() async {
  final cubit = CounterCubit();
  cubit.increment();
}

class CounterCubit extends Cubit<int> {
  CounterCubit() : super(0);

  void increment() async {
    emit(state + 1);
    print('State: $state');
    emit(state + 1);
    print('State: $state');
    //await Future.delayed(Duration(milliseconds: 500));

    emit(state + 1);
    print('State: $state');
  }
}

The output is:

State: 0
State: 0
State: 0

Now if I run the same code but with the Future uncomented then the last print shows "State: 1" and the complete output is:

State: 0
State: 0
State: 1

@felangel I can show my flutter code where my CubitBuilder is not being rebuilt in some cases when I run a emit(state) if it hepls in solving this issue.

@felangel
Copy link
Owner

felangel commented Jul 6, 2020

@pedromassango can you please try the above code using cubit ^0.2.0-dev.1 and let me know if you're still experiencing the same behavior? I believe your issue is a duplicate of #52.

I just tested and the above code outputs

State: 1
State: 2
State: 3

when using v0.2.0-dev.1 👍

@pedromassango
Copy link

@pedromassango can you please try the above code using cubit ^0.2.0-dev.1 and let me know if you're still experiencing the same behavior? I believe your issue is a duplicate of #52.

It is working on this version thanks. When we can espect this to be released?

@felangel
Copy link
Owner

felangel commented Jul 6, 2020

@pedromassango I'm working on a few other breaking changes currently but should have a stable 0.2.0 out sometime this week 👍

Feel free to use 0.2.0-dev.1, I've upgraded all cubit packages (including hydrated_cubit and replay_cubit) to be compatible with 0.2.0-dev.1 so you should be unblocked in the meantime 😄

@pedromassango
Copy link

This just saved my day. Thanks.

@md-weber
Copy link
Author

md-weber commented Jul 7, 2020

@felangel I added an own branch in one of my projects that includes the problem. You can run the app, select a drink and click the action icons to undo it.

https://github.com/md-weber/state_tutorials/tree/minimal_example

@felangel
Copy link
Owner

felangel commented Jul 7, 2020

Thanks @md-weber! I’ll have a look shortly 👍

@felangel
Copy link
Owner

felangel commented Jul 7, 2020

@md-weber just took a look and opened a pull request with the fix. The reason the replay_cubit wasn't working properly is because the cubit implementation was directly mutating the state rather than create new state instances.

Hope that helps 👍

@felangel felangel closed this as completed Jul 7, 2020
cubit automation moved this from To do to Done Jul 7, 2020
@felangel felangel removed the waiting for response Waiting for follow up label Jul 7, 2020
@md-weber
Copy link
Author

md-weber commented Jul 7, 2020

Fantastic thanks for that, now I feel quite stupid 😄. Ok, I understand now, thanks and keep up the great work :)

@felangel
Copy link
Owner

felangel commented Jul 7, 2020

@md-weber no worries at all it's a subtle detail haha. Keep up the awesome work on your Youtube channel! 🎉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
No open projects
cubit
  
Done
Development

No branches or pull requests

3 participants