I’m trying to save my data in flutter with sqflite package but it is not working

the code below is my riverpod provider file which I am using sqflite and path package in there:

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:path/path.dart' as path;
import 'package:sqflite/sqflite.dart' as sql;
import 'package:sqflite/sqlite_api.dart';

import 'package:coffee_shop_app/models/coffee.dart';

Future<Database> _getDatabase() async {
  final dbPath = await sql.getDatabasesPath();
  final db = await sql.openDatabase(
    path.join(dbPath, 'coffee.db'),
    onCreate: (db, version) {
      return db.execute(
          'CREATE TABLE cart_provider(id TEXT PRIMARY KEY, coffeeImage TEXT, CoffeeName TEXT, CoffeePrice REAL)');
    },
    version: 1,
  );
  return db;
}

class CartNotifier extends StateNotifier<List<Coffee>> {
  CartNotifier() : super([]);

  Future<void> loadCoffee() async {
    final db = await _getDatabase();
    final data = await db.query('cart_provider');
    final coffees = data
        .map(
          (row) => Coffee(
            id: row['id'] as String,
            coffeeImage: row['coffeeImage'] as String,
            coffeeName: row['coffeeName'] as String,
            coffeePrice: row['coffeePrice'] as double,
          ),
        )
        .toList();

    state = coffees;
  }

  void addCoffee(Coffee coffee) async {
    if (state.contains(coffee)) {
      return;
    } else {
      final db = await _getDatabase();
      db.insert(
        'cart_provider',
        {
          'id': coffee.id,
          'coffeeImage': coffee.coffeeImage,
          'coffeeName': coffee.coffeeName,
          'coffeePrice': coffee.coffeePrice,
        },
      );

      state = [...state, coffee];
    }
  }

  void removeCoffee(Coffee coffee) async {
    final db = await _getDatabase();
    await db.delete(
      'cart_provider',
      where: 'id = ?',
      whereArgs: [coffee.id],
    );

    state = state.where((c) => c != coffee).toList();
  }
}

final cartProvider = StateNotifierProvider<CartNotifier, List<Coffee>>(
  (ref) {
    return CartNotifier();
  },
);

and the code below is where I am using the provider to show listView:

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:coffee_shop_app/provider/cart_provider.dart';
import 'package:coffee_shop_app/components/registered_coffee_item.dart';

class CartScreen extends ConsumerStatefulWidget {
  const CartScreen({super.key});

  @override
  ConsumerState<CartScreen> createState() => _CartScreenState();
}

class _CartScreenState extends ConsumerState<CartScreen> {
  late Future<void> _coffeesFuture;

  @override
  void initState() {
    super.initState();
    _coffeesFuture = ref.read(cartProvider.notifier).loadCoffee();
  }

  @override
  Widget build(BuildContext context) {
    final registeredCoffees = ref.watch(cartProvider);

    return FutureBuilder(
      future: _coffeesFuture,
      builder: (context, snapshot) =>
          snapshot.connectionState == ConnectionState.waiting
              ? const Center(
                  child: CircularProgressIndicator(),
                )
              : Column(
                  children: [
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 20,
                          vertical: 20,
                        ),
                        child: ListView(
                          children: [
                            ...registeredCoffees.map((coffee) {
                              return RegisteredCoffeeItem(
                                coffee: coffee,
                                icon: Icons.delete,
                              );
                            }),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
    );
  }
}

the problem is that when I reload my app the data is not saved!

I checked my codes a few times and I watched some youtube videos. everything looks fine even in my debug console but when I reload my app the data is not saved.

New contributor

Ehsan Javdan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật