1 | initial version |
Here are the steps to access sensor data stored in the Firebase realtime database and display it on a Flutter application:
Set up your Firebase project and create a realtime database in it.
Connect your Flutter application to the Firebase project using the Firebase package. Add the following dependencies to your pubspec.yaml file:
dependencies:
firebase_core: ^1.10.0
firebase_database: ^8.2.3
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
import 'package:firebase_database/firebase_database.dart';
final databaseReference = FirebaseDatabase.instance.reference();
databaseReference.child('sensor').once().then((DataSnapshot snapshot) {
print('Data : ${snapshot.value}');
});
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
final databaseReference = FirebaseDatabase.instance.reference();
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sensor Data"),
),
body: Center(
child: FutureBuilder(
future: databaseReference.child('sensor').once(),
builder: (BuildContext context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
return Text('Sensor Data: ${snapshot.data!.value}');
} else {
return CircularProgressIndicator();
}
},
),
),
);
}
}