I would like to update the values in one page and get the updated value in next page.
here is my riverpod
provider:
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;
}
Future<void> addCoffee(Coffee coffee) async {
if (state.contains(coffee) == true) {
return;
} else {
final db = await _getDatabase();
await 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();
}
bool isAdded(Coffee coffee) {
if (state.contains(coffee)) {
return true;
} else {
return false;
}
}
}
final cartProvider = StateNotifierProvider<CartNotifier, List<Coffee>>((ref) {
final notifier = CartNotifier();
return notifier;
});
if you need more information please tell me.