TKK_E32221220/lib/services/database_service.dart

25 lines
756 B
Dart

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
class DatabaseService {
final uid = FirebaseAuth.instance.currentUser!.uid;
final db = FirebaseDatabase.instance.ref();
Future<void> saveBMI(double value) async {
final now = DateTime.now().toIso8601String();
await db.child("users/$uid/bmi_history").push().set({
"value": value,
"time": now,
});
}
Future<Map<String, double>> fetchLatestIoTData() async {
final snapshot = await db.child("iot/latest").get();
final data = Map<String, dynamic>.from(snapshot.value as Map);
return {
'height': (data['height'] as num).toDouble(),
'weight': (data['weight'] as num).toDouble(),
};
}
}