GithubHelp home page GithubHelp logo

Preserve intialState of a cubit about cubit HOT 5 CLOSED

felangel avatar felangel commented on July 28, 2024
Preserve intialState of a cubit

from cubit.

Comments (5)

felangel avatar felangel commented on July 28, 2024 1

What do you mean? It's one extra line of code haha. If you want, you can make your own custom cubit that does this for you like:

abstract class MyCubit<T> extends Cubit<T> {
  MyCubit(this.initialState) : super(initialState);
  final T initialState;
}

Then you can make your cubits all extend MyCubit instead:

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

  void someMethod() {
    // you can now access the initial state via `initialState`
  }
}

This solution is the same amount of code as you would currently write minus the MyCubit class (which is a one time thing) 👍

from cubit.

narcodico avatar narcodico commented on July 28, 2024 1

Hi @pedromassango 👋

It's not cubit's responsibility to cache your initial state.
You can have a factory constructor on your freezed data class

factory LanguageState.initial() => LanguageState(
      languages: [],
      isLoadingLanguages: true,
      loadLanguageFailure: null,
);

and use it to initialize your cubit or to emit it again when needed.

LanguageCubit() : super(LanguageState.initial());

emit(LanguageState.initial());

Hope that helps 👍

from cubit.

felangel avatar felangel commented on July 28, 2024

Hi @pedromassango 👋

You can add an initialState property to your cubit as needed 👍

class CounterCubit extends Cubit<int> {
  CounterCubit(this.initialState) : super(initialState);

  final int initialState;
}

Hope that helps 👍

from cubit.

pedromassango avatar pedromassango commented on July 28, 2024

Hi @pedromassango 👋

You can add an initialState property to your cubit as needed 👍

class CounterCubit extends Cubit<int> {
  CounterCubit(this.initialState) : super(initialState);

  final int initialState;
}

Hope that helps 👍

I mean, the cubit to preserve the initial state passed into it. The solution you suggested works but:

  • You need to provide it via constructor
  • You need to write a lot fo code for every Cubit in the project when neccessary.

from cubit.

pedromassango avatar pedromassango commented on July 28, 2024

I get it 👍

from cubit.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.