56 lines
1.6 KiB
Dart
56 lines
1.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:rogapp/model/destination.dart';
|
|
import 'package:rogapp/utils/database_helper.dart';
|
|
|
|
class DatabaseService {
|
|
// Private constructor
|
|
DatabaseService._privateConstructor();
|
|
|
|
// Static instance
|
|
static final DatabaseService _instance =
|
|
DatabaseService._privateConstructor();
|
|
|
|
// Factory constructor to return the instance
|
|
factory DatabaseService() {
|
|
return _instance;
|
|
}
|
|
|
|
// StreamController for updates
|
|
final StreamController<List<Destination>> _dbUpdateController =
|
|
StreamController<List<Destination>>.broadcast();
|
|
|
|
// Getter for the stream
|
|
Stream<List<Destination>> get destinationUpdatesStream =>
|
|
_dbUpdateController.stream;
|
|
|
|
// Method to fetch destinations
|
|
Future<List<Destination>> fetchDestinations() async {
|
|
// Your database fetch logic here
|
|
List<Destination> destinations = await _fetchFromDb();
|
|
_dbUpdateController.add(destinations);
|
|
return destinations;
|
|
}
|
|
|
|
// Method to update the database and emit an update through the stream
|
|
Future<void> updateDatabase() async {
|
|
// Your update logic here
|
|
|
|
// After updating, fetch the updated list and add it to the stream
|
|
fetchDestinations();
|
|
}
|
|
|
|
// Method to fetch data from the database (placeholder)
|
|
Future<List<Destination>> _fetchFromDb() async {
|
|
// Simulate fetching data with a delay
|
|
DatabaseHelper db = DatabaseHelper.instance;
|
|
List<Destination> destinations = await db.getDestinations();
|
|
return destinations; // Replace with your actual fetch operation
|
|
}
|
|
|
|
// Dispose method to close the stream controller
|
|
void dispose() {
|
|
_dbUpdateController.close();
|
|
}
|
|
}
|