StickyGroupedListView Range Error: Not in inclusive range 0..1 : 2

80 Views Asked by At

I have made a chat app using stickygroupedlistview and back4app as api in flutter. I use FutureBuilder to retrieve the data and group by date and time. The messages are grouped by date and time with sticky headers. I keep getting range error when I try to add or remove message from the list.

I am using stateful widget and call setstate everytime I add or remove message in the list but it keeps giving range error. I tried using a UniqueKey since it reloads the list everytime setstate is called, it solves the range error but the list keeps going back to the first element every time setstate is called. Has anyone else faced this issue with stickygroupedlistview package? Any help would be highly appreciated.

Here is my code.

 Widget _messageSpace() {
    return Column(
      children: [
        Expanded(
          child: Padding(
            padding: const EdgeInsets.only(left: 10, right: 10),
            child: FutureBuilder<List<dynamic>?>(
                future: _loadMessages(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (snapshot.hasData) {
                    results = snapshot.data ?? [];


                    return StickyGroupedListView<dynamic, DateTime>(
                      elements: results,
                      reverse: true,
                      order: StickyGroupedListOrder.DESC,
                      groupBy: (dynamic message) {
                        if (message.createdAt != null) {
                          return DateTime(
                              message.createdAt!.toLocal().year,
                              message.createdAt!.toLocal().month,
                              message.createdAt!.toLocal().day);
                        } else {
                          return DateTime(DateTime.now().year,
                              DateTime.now().month, DateTime.now().day);
                        }
                      },
                      floatingHeader: true,
                      groupComparator: (DateTime value1, DateTime value2) {
                        return value1.toLocal().compareTo(value2.toLocal());
                      },
                      itemComparator: (dynamic element1, dynamic element2) {
                        if (element1.createdAt != null &&
                            element2.createdAt != null) {
                          return element1.createdAt!
                              .toLocal()
                              .compareTo(element2.createdAt!.toLocal());
                        } else if (element1.createdAt == null &&
                            element2.createdAt != null) {
                          return DateTime.now()
                              .compareTo(element2.createdAt!.toLocal());
                        } else if (element1.createdAt != null &&
                            element2.createdAt == null) {
                          return element1.createdAt!
                              .toLocal()
                              .compareTo(DateTime.now());
                        } else {
                          return DateTime.now().compareTo(DateTime.now());
                        }
                      },
                      groupSeparatorBuilder: (dynamic element) {
                        
                        return Padding(
                          padding: const EdgeInsets.only(bottom: 0, top: 3),
                          child: TextWithTap(
                            QuickHelp.getMessageTime(element.createdAt != null
                                ? element.createdAt!.toLocal()
                                : DateTime.now()),
                            textAlign: TextAlign.center,
                            color: kGreyColor1,
                            fontSize: 12,
                          ),
                        );
                      },
                      itemBuilder: (context, dynamic chatMessage) {
                        bool isMe = chatMessage.getAuthorId! ==
                                widget.currentUser!.objectId!
                            ? true
                            : false;

This is how I'm adding message in the results list

saveMessage(){
MessageModel message = MessageModel();
 setState(() {
        results.insert(0, message as dynamic);
      });

      await message.save();
}

And this is how I'm removing the message from the results list

removeMessage(MessageModel message){
setState((){
results.remove(message);
});
}
0

There are 0 best solutions below