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

ObservableList / ObservableMap not rebuild ui after mutate property #1026

Open
Poloten opened this issue Nov 12, 2024 · 0 comments
Open

ObservableList / ObservableMap not rebuild ui after mutate property #1026

Poloten opened this issue Nov 12, 2024 · 0 comments

Comments

@Poloten
Copy link

Poloten commented Nov 12, 2024

ChatItem is class without the Store , I attempt to mutate propery in ObservableList and ObservableMap but tracking changes not working. I tried with chatStore.chats.toList() and without toList(), and add observer inside itemBuilder, but nothing happend. UI updating only if i change the route or reassing whole chatsMap / chats. Please can u help me with this simple task.

I also extend ChatItem class with equatable package to override hashCode and operator ==
I use .g generator

mobx: ^2.4.0
flutter_mobx: ^2.2.1+1
mobx_codegen: ^2.6.2

class ChatsStore = _ChatsStore with _$ChatsStore;
abstract class _ChatsStore with Store {

@MakeObservable(useDeepEquality: true)
ObservableList<ChatItem> chats = ObservableList.of([]);
@MakeObservable(useDeepEquality: true)
ObservableMap<String, ChatItem> chatsMap = ObservableMap<String, ChatItem>.of({});
@computed
ObservableList<ChatItem> get computedChatList => ObservableList.of(chatsMap.value);

@action
Future<void> getChats() async {
  List<ChatItem>? chatList = await getChatsList();
  if (chatList == null) return;

  // try List
  chats.clear();
  chats.addAll(chatList);

   // try Map
  chatsMap.clear();
  for (var chat in chatList) {
    chatsMap[chat.id] = chat;
  }
}

/// I tried to update property inside **ChatItem**
@action
updateUnread(String id) {
  print('hash before: ${ chatsMap[id].hashCode}');
  chatsMap[id].unreadCount = 99;
  print('hash after: ${ chatsMap[id].hashCode}'); // hash changed
  
  var found = chats.firstWhere((el) => el.id == id);
  found.unreadCount = 98;
}
}

Widget

Column(children: [
        Expanded(
          child: Observer(builder: (_) {
            var chatList = chatStore.chats.toList();
            print('REBUILD');
            return RefreshIndicator(
                onRefresh: handleRefresh,
                color: Colors.white,
                backgroundColor: Colors.blue,
                child: ListView.separated(
                  separatorBuilder: (_, int index) => const DididerWmKeyVal(),
                  itemCount: chatList.length,
                  itemBuilder: (_, int index) {
                    var element = chatList[index];
                    return InkWell(
                      child: SizedBox(
                        height: 70,
                        child: Row(
                          children: [
                            Container(
                              padding: EdgeInsets.only(top: 10),
                              alignment: Alignment.topCenter,
                              width: 60,
                              child: Stack(
                                children: [
                                  AvatarWidget(wmid: element.id),
                                  BadgeWidget(count: element.unreadCount)
                                ],
                              ),
                            ),
                            Expanded(
                              child: Padding(
                                padding: EdgeInsets.only(top: 4),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(element.name),
                                    Text('${element.hint}'.trim()),
                                  ],
                                ),
                              ),
                            ),
                            ListItemTime(time: element.time),
                          ],
                        ),
                      ),
                      onTap: () {
                        chatStore.updateUnread("78000000");
                  },
                ));
          }),
        ),
      ]),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant