Error Im getting is - Null Function(DataSnapshot) can;t be assigned to the parameter type 'FutureOr Function(DatabaseEvent)'Im trying to retrieve data from firebase but it is not happening.
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final textcontroller = TextEditingController();
final databaseRef = FirebaseDatabase.instance.reference();
final Future<FirebaseApp> _future = Firebase.initializeApp();
//FirebaseAuth auth = FirebaseAuth.instance;
void addData(String data) {
databaseRef.child("Valve").set({Flow : 'ON'});
}
void addData2(String data) {
databaseRef.child("Valve").set({Flow : 'OFF'});
}
void ReadData_once() {
databaseRef.child("FirebaseIOT").child("LatestReading").once().then((DataSnapshot Snapshot) {
print();
});//this is the error
}
void printFirebase(){
databaseRef.once().then((DataSnapshotsnapshot) {
print('Data : ');
});
}//this is where error is coming
CodePudding user response:
Instead of DataSnapshot, you should put DatabaseEvent within the then
So this part
databaseRef.child("FirebaseIOT").child("LatestReading").once().then((DataSnapshot Snapshot) {print('test')}
should be
databaseRef.child("FirebaseIOT").child("LatestReading").once().then((DatabaseEvent event) {print('test')}
CodePudding user response:
I am not a dart/flutter programmer but i know that you have to initialize app before any other modules you want to use from firebase library.
So try to switch this lines:
final Future<FirebaseApp> _future = Firebase.initializeApp();
final databaseRef = FirebaseDatabase.instance.reference();
And i'm pretty sure initializeApp() needs firebase config object so you should initialize like this: firebase.initializeApp(firebaseConfig).
I don't know what is this: Future<FirebaseApp> why you force to get initialization result to be this future generic object ?
