getter 'snapshot' 被调用为 null。接收者:空尝试调用:快照

时间:2021-04-07 03:50:30

标签: android firebase flutter firebase-realtime-database iot

如何解决。 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});
  }
}

1 个答案:

答案 0 :(得分:-1)

    ======== 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
====================================================================================================