brianegan/flutter_architecture_samples

What would cause a Flutter Redux StoreConnector to not rebuild.

Open

#76 aperta il 19 ott 2018

Vedi su GitHub
 (5 commenti) (0 reazioni) (0 assegnatari)Dart (1717 fork)batch import
awaiting replygood first issue

Metriche repository

Star
 (8920 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

I try to follow the issues religiously on the flutter_architecture_sample. Thank you, to all of you that contribute and ask questions. It helps a lot. I have not seen an issue like this and I could not find the right search query to bring up anything close to my problem. I would really appreciate some help if anyone has an idea as to why this StoreBuilder is not rebuilding. The underlying state of the reducer is two Map<String, Whatevers>'s. I tried to add code without overwhelming the post.

    #View
    return ListView.builder(
                itemCount: eventIDs.length,
                itemBuilder: (BuildContext context, int i) {
                  return StoreConnector<AppState, EventsPageViewModel>(
                      rebuildOnChange: true,
                      onDidChange: (EventsPageViewModel events) {print('${events}');},
                      onInit: (store) =>
                          store.dispatch(FetchEventAction(id: eventIDs[i])),
                      converter: (store) => EventsPageViewModel.fromStore(store),
                      builder: (BuildContext context, EventsPageViewModel events) {
                        print("build ListItem $i ${eventIDs[i]}");
                        if (events.events.containsKey('${eventIDs[i]}')) {
                          return EventCard(
                            event: events.events[eventIDs[i]],
                          );
                        }
                        return Container(
                          child: Card(
                            child: Container(
                              height: 120.0,
                            ),
                          ),
                        );
                      });
                },
              );
    #State
    @immutable
    class Events {
      Events({
        this.loadingStatus,
        this.events,
      });

      factory Events.initial() {
        return Events(
          loadingStatus: Map<String, LoadingStatus>(),
          events: Map<String, EventModel>(),
        );
      }

      final Map<String, LoadingStatus> loadingStatus;
      final Map<String, EventModel> events;

      Events copyWith({
        Map<String, LoadingStatus> loadingStatus,
        Map<String, EventModel> events,
      }) {
        return Events(
          loadingStatus: loadingStatus ?? this.loadingStatus,
          events: events ?? this.events,
        );
      }

      @override
      bool operator ==(Object other) =>
          identical(this, other) ||
          other is Events &&
              runtimeType == other.runtimeType &&
              loadingStatus == other.loadingStatus &&
              const MapEquality().equals(events, other.events);

     @override
     int get hashCode => loadingStatus.hashCode ^ const MapEquality().hash(events);
    }


    #ViewModel
    class EventsPageViewModel {
      EventsPageViewModel({
       @required this.status,
       @required this.events,
     });

     final Map<String, LoadingStatus> status;
     final Map<String, EventModel> events;

     static EventsPageViewModel fromStore(
       Store<AppState> store,
     ) {
       return EventsPageViewModel(
         status: store.state.events.loadingStatus,
         events: store.state.events.events,
       );
     }

     @override
     bool operator ==(Object other) =>
         identical(this, other) ||
         other is EventsPageViewModel &&
             runtimeType == other.runtimeType &&
             status == other.status &&
             const MapEquality().equals(events, other.events);

     @override
     int get hashCode => status.hashCode ^ const MapEquality().hash(events);
    }

I'm trying to get a view to rebuild after it fire off an async action and it return a value to a Map. I don't know if i'm doing the equality check properly

I checked to see if the middleware was firing off the GET Request. It is. I checked to see if the GET request was returning information. It is. I have another async action that is firing. That one returns a List of string and does trigger a rebuild on its StoreConnector.

Does anyone have any clues as to why the StoreConnector does not detect a diff?

Guida contributor