List item is not deleting from Hive Box

56 Views Asked by At

I have List<Notifications> in Hive Box and dispaying the notifications using ListView.builder. I use Dismissible() to delete the notification from the page. I used await box.delete(notification.id); but,it is not deleting. Please solve the problem.

class NotificationsPage extends StatefulWidget {
  const NotificationsPage({super.key});

  @override
  _NotificationsPageState createState() => _NotificationsPageState();
}

class _NotificationsPageState extends State<NotificationsPage> {
  late List<Notifications> notifications = box.values.toList();


  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          appBar: AppBar(
            title: Text('Notifications'),
          ),
          body: Padding(
            padding: EdgeInsets.symmetric(vertical: 15.h),
            child: Column(
              children: [
                Expanded(
                  child: ValueListenableBuilder<Box>(
                    valueListenable: box.listenable(),
                    builder: (context, boxes, widget) {
                      notifications = box.values.toList();

                      return ListView.builder(
                        itemCount: notifications.length,
                        itemBuilder: (context, index) {
                          final notification = notifications[index];

                          return Dismissible(
                            key: Key(notification.id.toString()),
                            onDismissed: (direction) async {
                              await box.delete(notification.id);
                            },
                            child: Card(
                              child: ListTile(
                                title: Text(notification.title),
                                subtitle: Text(notification.subTitle),
                                leading:
                                    CustomImageFrame(icon: notification.icon),
                              ),
                            ),
                          );
                        },
                      );
                    },
                  ),
                ),
                Center(
                    child: InkWell(
                  onTap: () {
                    delete();
                  },
                  child: Icon(Icons.delete),
                ))
              ],
            ),
          )),
    );
  }
}

function for saveNotification


saveNotifications(String id, String title, String subTitle, String category,
    String icon) async {
  final box = Hive.box<Notifications>('Notifications');
  Notifications notification = Notifications(
      id: id, title: title, subTitle: subTitle, category: category, icon: icon);
  await box.add(notification);
}

Notification and Adapter class

@HiveType(typeId: 1)
class Notifications extends HiveObject {
  @HiveField(0)
  final String id;

  @HiveField(1)
  final String title;

  @HiveField(2)
  final String subTitle;

  @HiveField(3)
  final String category;

  @HiveField(4)
  final String icon;

  Notifications({required this.id, required this.title, required this.subTitle, required this.category, required this.icon});
}

class NotificationsAdapter extends TypeAdapter<Notifications> {
  @override
  final typeId = 1;


  @override
  Notifications read(BinaryReader reader) {
    final id = reader.readString();
    final title = reader.readString();
    final subTitle = reader.readString();
    final category = reader.readString();
    final icon = reader.readString();
    return Notifications(
        id: id,
        title: title,
        subTitle: subTitle,
        category: category,
        icon: icon);
  }

  @override
  void write(BinaryWriter writer, Notifications obj) {
    writer.writeString(obj.id);
    writer.writeString(obj.title);
    writer.writeString(obj.subTitle);
    writer.writeString(obj.category);
    writer.writeString(obj.icon);
  }
}

0

There are 0 best solutions below