-1

i'm new to flutter and i'm having some trouble with data managment using bloc. I`m building an app that make a request(get_things) using bloc and recieve as response a list of objects( example: {"things": [{"id": 1, "color" : "green"},{"id":2 , "color":"blue"},...] ).

With this response i build a widget presenting these "things" using blocState. Then i make another page that calls the same bloc to create a similar widget,but in this page we can create, modify or delete "things" and make the request to change them on the database (modify_things).

The problem is i'd like both pages to update the list after a change without making a request to backend to rebuild the lists with the modified list.

What i though as a solution was to save the blocState data of the initial request somewhere and then use this locally to make the changes to the widgets(lists). What i mean is, i delete one "thing" and send the request to modify the database, and at the same time i modify the saved blocState list locally to reflect this change. This way i only have to use get_things once and modify_things only when i make changes to the list.

Is this a good aproach? this method seems cumbersome, but as i'm fairly new to flutter so i couldn't think of anything else.

As for what i have the need to make less requests and manage this locally, it's because i've been told that in the future these "things" can get really big, and that making a request everytime i need to see the widget could eventually be a performance issue.

1 Answers1

0

If I understand you correctly, you send [{"id": 1, "color" : "green"},{"id":2 , "color":"blue"},...] to page A and then you want to use the same data on page B.

The clean way, I guess, would be to add an event on page A with myBloc.add([{"id": 1, "color" : "green"},{"id":2 , "color":"blue"},...] ) which also triggers page B to load. Then myBloc yields a state with that and potentially more data, showing page B.

But of course you could also declare a PageADisplayState storedState; in myBloc and then before yielding the state store it by storedState = things; yield PageADisplayState(things);

w461
  • 2,168
  • 4
  • 14
  • 40
  • i already get the data on page B by calling the page A bloc that make the request get_things. Then i make a widget that allows me to select a "thing", edit it and then send the request edit_things to reflect the change on my database. What im trying to do is to get this new modified list of things to display in both pages without the need to make a request everytime i refresh the page. I guess what i'm asking is if the data stored on my blocState can be modified without checking the backend for changes. Please let me know if i'm not being clear enough. – Iván Ezequiel Vargas Dec 30 '20 at 16:29
  • If you have 2 blocs, you can use my Q&A here to send data back to blocA: https://stackoverflow.com/questions/64011595/flutter-inter-bloc-communication-passing-data-events-between-different-blocs. To not trigger a new backend request I sometimes simply store the data as a var in that bloc and use `if (myVar == null) then myVar = getData()`. So data is pulled on first execution, then I reuse or manipulate that data – w461 Dec 30 '20 at 17:33
  • What a coincidence! I was just thinking about doing exactly that. I think my problem was thinking that this was a messy solution. I'm trying to use clean architecture with bloc, so the idea of intentionally avoid using the blocState for everything is a bit alien to me. I appreciate the input. – Iván Ezequiel Vargas Dec 30 '20 at 17:46
  • I will ! But before that i have another question you may be able to help with. i'm having trouble managing myVar with the list inside. I'd like to use it to build the widgets in these pages that manage my "things". i've been reading about state managment a bit but i'm a clueless about how to make myVar is shared across the pages so when it's modified(edit or delete "things"), every page can reflect the change after a refresh. It should work like some "global variable" across the pages that involve this list of "things". I appreciate the help! – Iván Ezequiel Vargas Dec 31 '20 at 13:50