1

I have a screen that displays

  1. a stock value of an asset
  2. an asset selection dropdown

For now, I put all those values in a singe State class:

class AssetsLoaded extends AssetsState {
  final List<ActiveSymbol> assets;
  List<String> get markets {
    return assets.map((e) => e.market).toSet().toList();
  }
  String selectedMarket;
  ActiveSymbol selectedAsset;
  int selectedAssetPrice;

  AssetsLoaded({this.assets, this.selectedMarket, this.selectedAsset, this.selectedAssetPrice});```
}

Should I separate this State class into several smaller State classes in Cubit architecture? E.g. assets list seem unrelated to selection information. Should I keep all the variables that are consumed by the screen in one state, or should I create several smaller states and cubits?

enter image description here

Alehar
  • 639
  • 5
  • 16

1 Answers1

1

Generally, you'll want to keep related data together. Since these two bits of information are both related to assets, they should be kept in the same Cubit/Cubit state. This means they can be accessed easily together, alongside if you need to return both at the same time, some version of both, or modify one in relation to another.

If instead you split them up, you may need to introduce loose coupling, which isn't great.

Matthew Trent
  • 2,611
  • 1
  • 17
  • 30
  • I see following groups here: 1. asset list 2. asset selection 3. asset price. If I understand you correctly, I shouldn't create separate states/cubits for them? – Alehar Jan 19 '23 at 20:36
  • In this case, how would I easily manage the information that Asset price is loaded or not? With asset list I check for type "is AssetsLoaded". How would I check if Asset price is loaded? It takes an async operation to load that data. – Alehar Jan 19 '23 at 20:38
  • 1
    Correct, as if you split those up between multiple states, they will be much harder to change in relation to each other. For reference, my states for Cubits in the apps I build generally have 3 core states: LoadingState, ErrorState, DataState. For you, all of that assets-related data probably will go inside something akin to a DataState. Aka, 1 state and cubit. – Matthew Trent Jan 19 '23 at 20:41
  • 1
    (Above comment posted before loading your second comment.) But, I'm assuming that you'd load all your asset-data in one sort of async "operation", where that DataState has all of the asset-related data together, and the other states stand for error and loading of such grouped data. I suppose if you want to load all of them separately you could split them up? Although, that would result in a whole bunch of extra states - it could be more confusing. – Matthew Trent Jan 19 '23 at 20:44