Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.select not updating with a list item that goes from null to having data #3739

Closed
mark8044 opened this issue Sep 12, 2024 · 3 comments
Closed
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@mark8044
Copy link

mark8044 commented Sep 12, 2024

I have a list of lists of maps inside a ChangeNotifier.

The idea is that I have API containing a list of news articles. The list is paginated by the API so an API call request is made specifiying which page of data should be sent.

I previously had a large map of the data, where the map would keep growing as more pages of data came in. This is problematic because my list views would rebuild in entirety as the map kept getting changed.

So I've decided to divide each page of API data into its own small listview/column and just add those columns into a larger customscrollview as they come in

To do this, Ive created a larger container pageMaps list which holds each page of data. pageMaps[1] is page 1 of data, etc.. Each pageMap List item contains about 20 articles within it

class MyData extends ChangeNotifier
{

List<List<dynamic>> _pageMaps = [[]];
List<List<dynamic>> get pageMaps => _pageMaps;

// some logic that adds items to _pagemaps via API network call
_pageMaps.add(apiresponse.data["mynewdata"]);
}

Now in my Widget, I try accessing this data with a select:

     if (ref.watch(MyProvider(familyitemID).select((obj) => obj.pageMaps)).elementAtOrNull(widget.page) != null)
      {
        debugPrint('i select');
        thisPageMap = ref.watch(MyProvider(familyitemID).select((obj) => obj.pageMaps[widget.page]));
      }
     else
     {
     debugPrint('im null');
      thisPageMap = [];
     }

When the page first builds it properly shows null as the data has not come in from my API network call, once it populates, the Widget never rebuilds, ie. I select is never printed.

Of course If I change the logic to be a straight .watch on pageMaps, without the select then it will update my widget properly as the API updates. However I find myself back where I started as whenever pageMaps[2] is updated, the widget rebuilds for lets say pageMaps[1] which is what im trying to avoid.

Im trying to specifically look for a change in pageMaps[x] and update only then. I guess pageMaps[x] would be considered mutable and that might be the issue here? But surely there should be a way to select for an item in a list when there is a state change and not disrupt all the other pages of data relying on it?

@mark8044 mark8044 added bug Something isn't working needs triage labels Sep 12, 2024
@rrousselGit
Copy link
Owner

Your select isn't quite right. You're selecting the whole map, even though you're mutating it.
You probably want something like:

final page = ref.watch(provider.select((obj) => obj.pageMaps[widget.page]));
if (page != null) {

} else {

}

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels Sep 12, 2024
@mark8044
Copy link
Author

mark8044 commented Sep 12, 2024

@rrousselGit yes I prefer to select the Listitem directly to avoid rebuild, but it seems impossible with an empty list (while waiting for API data to fill in). However, the .elementAtOrNull helps me avoid a RangeError that I get with a list when empty while waiting for the API result

RangeError (length): Invalid value: Only valid value is 0: 1

@rrousselGit
Copy link
Owner

Right. I thought you used a map. But that doesn't change the answer.

Don't:

watch(provider.select((obj) => obj.field).method());

Do:

watch(provider.select((obj) => obj.field.method()));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants