i was trying to connect my firebase with my flutter app but got the error
"The property 'snapshot' can't be unconditionally accessed because the receiver can be 'null'." and "The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!')"
it has an error in
snapshot.data.snapshot.value["Temperature:"]
and
snapshot.data.snapshot.value["Humidity:"]
My full code:
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class IOTScreen extends StatefulWidget {
const IOTScreen({Key? key}) : super(key: key);
@override
_IOTScreenState createState() => _IOTScreenState();
}
class _IOTScreenState extends State<IOTScreen> {
bool value = false;
bool values = false;
final dbRef = FirebaseDatabase.instance.ref();
@override
void initState() {
super.initState();
initializeFirebase();
}
void initializeFirebase() async {
try {
await Firebase.initializeApp();
print('Firebase initialized successfully');
} catch (e) {
print('Failed to initialize Firebase: $e');
}
}
void onUpdate() {
setState(() {
value = !value;
});
}
void onUpdate2() {
setState(() {
values = !values;
});
}
void writeData() {
dbRef.child("Data").set({"Humidity: ": 0, "Temperature: ": 0});
dbRef.child("LightStates").set({"switch": !value});
}
void writeData2() {
dbRef.child("FanStates").set({"switch": !values});
}
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
return Scaffold(
body: StreamBuilder(
stream: dbRef.child("Data").onValue,
builder: (context, snapshot) {
if (snapshot.hasData &&
!snapshot.hasError &&
snapshot.data?.snapshot.value! != null) {
return Column(
children: [
Padding(
padding: const EdgeInsets.all(18),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(Icons.clear_all, size: 30),
Text(
"Egg Incubator",
style: TextStyle(
fontSize: 30, fontWeight: FontWeight.bold),
),
Icon(Icons.settings, size: 30)
],
),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Temperature",
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
snapshot.data.snapshot.value["Temperature:"],
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
),
)
],
)
],
),
SizedBox(height: 20),
Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Humidity",
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
snapshot.data.snapshot.value["Humidity:"],
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold),
),
)
],
),
SizedBox(
height: 60,
),
FloatingActionButton.extended(
onPressed: () {
writeData();
onUpdate();
},
label: value ? Text("Lamp ON") : Text("Lamp OFF"),
elevation: 20,
backgroundColor: value ? Colors.yellow : Colors.white60,
icon: value
? Icon(Icons.wb_incandescent)
: Icon(Icons.wb_incandescent_outlined)),
SizedBox(
height: 60,
),
FloatingActionButton.extended(
onPressed: () {
onUpdate2();
writeData2();
},
label: values ? Text("Fan OFF") : Text("Fan ON"),
elevation: 20,
backgroundColor:
values ? Colors.white60 : Colors.blueAccent,
icon: values
? Icon(Icons.wind_power_rounded)
: Icon(Icons.wind_power_outlined),
)
],
);
} else
return Container();
},
),
);
}
}
please let me know if you need any explanation. thankyou