从FutureBuilder <DataSnapshot>对null调用了getter'value'

时间:2020-08-25 12:02:55

标签: firebase flutter dart firebase-realtime-database

enter image description here

我可以从这两件事(Firebase Realtime Database的数据结构和下面的代码)获得结果。

enter image description here

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class GetMealScreen extends StatefulWidget {
  @override
  _GetMealScreenState createState() => _GetMealScreenState();
}

class _GetMealScreenState extends State<GetMealScreen> {

  DatabaseReference itemRef = FirebaseDatabase.instance.reference().child("testtest").child("test");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body:
        Container(
          child:
          FutureBuilder<DataSnapshot>(
            future: itemRef.once(),
            builder: (context, snapshot) {

              List<String> menuList = [
                snapshot.data.value["tt"].toString(),
              ];

              final double card_width = 370;
              final double card_upper_height = 210;

              return
                Container(
                  height: card_upper_height,
                  width: card_width,
                  child:
                  Padding(
                    padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0),
                    child: Swiper(//
                      control: SwiperControl(
                        color: Colors.white,
                      ),
                      pagination: SwiperPagination(
                          alignment: Alignment.bottomRight
                      ),
                      itemCount: menuList.length,
                      itemBuilder: (BuildContext context, int index){
                        return
                          Text(
                            menuList[index],
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              fontSize: 40,
                              color: Colors.black,
                            ),
                          );
                      },
                    ),
                  ),
                );
            },
          ),
        ),
    );
  }
}

但是问题是,每当我尝试访问此屏幕时,都会出现以下错误。

Performing hot reload...
Syncing files to device AOSP on IA Emulator...
Reloaded 5 of 685 libraries in 702ms.
I/flutter (15990): hi

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building FutureBuilder<DataSnapshot>(dirty, state: _FutureBuilderState<DataSnapshot>#84ed7):
The getter 'value' was called on null.
Receiver: null
Tried calling: value

The relevant error-causing widget was: 
  FutureBuilder<DataSnapshot> file:///Users/mingukkim/tubuc1.1.3/lib/GetMealMenu.dart:21:11
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      _GetMealScreenState.build.<anonymous closure> (package:Tubuc/GetMealMenu.dart:26:31)
#2      _FutureBuilderState.build (package:flutter/src/widgets/async.dart:751:55)
#3      StatefulElement.build (package:flutter/src/widgets/framework.dart:4683:28)
#4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4566:15)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

我认为它是因为snapshot.data.value["tt"].toString()返回空值而出现的,但事实并非如此,因此我无法理解问题所在。此外,我在Google上发现了类似的问题,但我无法找到像我这样的FutureBuilder的问题。你能告诉我怎么了吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

if(snapshot.hasData){
  List<String> menuList = [
                snapshot.data.value["tt"].toString(),
              ];

              final double card_width = 370;
              final double card_upper_height = 210;

              return
                Container(
                  height: card_upper_height,
                  width: card_width,
                  child:
                  Padding(
                    padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0),
                    child: Swiper(//
                      control: SwiperControl(
                        color: Colors.white,
                      ),
                      pagination: SwiperPagination(
                          alignment: Alignment.bottomRight
                      ),
                      itemCount: menuList.length,
                      itemBuilder: (BuildContext context, int index){
                        return
                          Text(
                            menuList[index],
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              fontSize: 40,
                              color: Colors.black,
                            ),
                          );
                      },
                    ),
                  ),
                );
            },
       // By default, show a loading spinner.
        return CircularProgressIndicator();
         },
          }

由于FutureBuilder用于异步操作,因此在从数据库中获取值后,请使用hasData使其进入snapshot.data.value["tt"].toString(),