Search code examples
flutterdartblocflutter-cubit

How to create BLoC initial state based on dynamic JSON response


In my Flutter project I want to initialise BLoC (Provider/Cubit) once API responds with definition of shape of the state required for entire app.

Consider following shapes

shape-1

{
  arr1: [],
  arr2: [],
  selected: false
}

shape-2

{
  arr1: [],
  obj1: {},
  selectedChoiceId: 8
}

As shown above there is no fixed shape of object in the response, hence state of application should be initialised once API responds.

I am using Flutter with BLoC configuration as per recommended by official documentation.

Please suggest how can I achieve "Dynamic state initialisation" with BloC.


Solution

  • As per my understanding you want to save your dynamic data state in whole app even you are in widget tree or not. so my suggestions are use ServiceLocator Get_IT to create global object of on of your data class where you can save your dynamic data inside map (var mapData = <String, dynamic>{}) with unique key so later you can retrieve that data and manipulate