![](/img/trans.png)
[英]Flutter : NoSuchMethodError: The getter 'nom' was called on null. Receiver; null Tried calling: nom
[英]The getter 'snapshot' was called on null. Receiver: null Tried calling: snapshot
如何修復它。 在 null 上調用了 getter 'snapshot'。 接收器:null 嘗試調用:快照我不知道,如何修復。對我來說這是一個新錯誤。我是新手。 這個項目是 Flutter + Firebase。
======== Exception caught by gesture ===============================================================
The following NoSuchMethodError was thrown while handling a gesture:
The getter 'snapshot' was called on null.
Receiver: null
Tried calling: snapshot
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 _IotScreenState.build.<anonymous closure>.<anonymous closure> (package:ioth1/screens/iotscreen.dart:127:44)
#2 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:991:20)
#3 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
#4 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:607:11)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#76fb3
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(220.0, 450.0)
finalLocalPosition: Offset(62.8, 39.1)
button: 1
sent tap down
====================================================================================================
我認為這行有錯誤
snapshot.data.snapshot.value["Temperature:"]
這是代碼。
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
class IotScreen extends StatefulWidget {
@override
_IotScreenState createState() => _IotScreenState();
}
class _IotScreenState extends State<IotScreen> {
bool value = false;
final dbRef = FirebaseDatabase.instance.reference();
onUpdate() {
setState(() {
value = !value;
});
}
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
return SafeArea(
child: Scaffold(
body:
StreamBuilder(
builder: (context, snapshot) {
if (snapshot.hasData &&
!snapshot.hasError &&
snapshot.data.snapshot.value != null) {
return Column(
children: [
Padding(
padding: const EdgeInsets.all(18.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
GestureDetector(
onTap: () {
},
child: Icon(
Icons.clear_all,
color: !value ? Colors.white : Colors.yellow,
),
// ),
),
Text("MY ROOM",
style: GoogleFonts.robotoSlab(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold)),
Icon(
Icons.settings,
color: !value ? Colors.white : Colors.yellow,
),
// ),
],
),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("Temperature",
style: TextStyle(
color: !value
? Colors.white
: Colors.yellow,
fontSize: 20,
fontWeight: FontWeight.bold)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
snapshot.data.snapshot.value["Temperature:"]
.toString() +
"°C",
style: TextStyle(
color: Colors.white, fontSize: 20)),
),
],
),
],
),
SizedBox(height: 80),
Padding(
padding: const EdgeInsets.all(18.0),
child: FloatingActionButton.extended(
icon: value
? Icon(Icons.visibility)
: Icon(Icons.visibility_off),
backgroundColor: value ? Colors.yellow : Colors.white,
label: value ? Text("ON") : Text("OFF"),
elevation: 20.00,
onPressed: () {
onUpdate();
writeData();
},
),
),
],
);
}
else {
return Center(
child: Container(
child: FloatingActionButton.extended(
icon: value
? Icon(Icons.visibility)
: Icon(Icons.visibility_off),
backgroundColor: value ? Colors.yellow : Colors.white,
label: value ? Text("ON") : Text("OFF"),
elevation: 20.00,
onPressed: () {
onUpdate();
writeData();
print(snapshot.data.snapshot.value["Temperature:"].toString());
},
),
),
);
}}
),
)
);
}
Future<void> writeData() async {
dbRef.child("Date").set({"Temperature": 0});
dbRef.child("LightState").set({"switch": value});
}
}
======== Exception caught by gesture ===============================================================
The following NoSuchMethodError was thrown while handling a gesture:
The getter 'value' was called on null.
Receiver: null
Tried calling: value
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 _IotScreenState.build.<anonymous closure>.<anonymous closure> (package:ioth1/screens/iotscreen.dart:127:54)
#2 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:991:20)
#3 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
#4 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:607:11)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#aa30f
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(218.0, 435.4)
finalLocalPosition: Offset(57.8, 24.5)
button: 1
sent tap down
====================================================================================================
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.