Flutter:NoSuchMethodError:在null上调用了getter'length'

问题描述

我正在尝试从Firebase实时数据库获取一些数据。这是我的代码,目的是通过http请求获取数据。我也正在接收数据,并且可以在日志中看到它。

return http.get(placeUrl).then((value) {
  var _extractedData = jsonDecode(value.body) as Map<String,dynamic>;
  print("Data: $_extractedData");
  _extractedData.forEach((_placeName,_details) {
    print("Details: $_details");
    var _events = _details['events'] as Map<String,dynamic>;
    List<EventModel> events = [];
    _events != null
        ? _events.forEach((_event,_eventDetail) {
            print("_eventdetail: $_eventDetail");
            List<PriceModel> prices = [];
            var _price = _eventDetail['price'] as Map<String,dynamic>;
            _price != null
                ? _price.forEach((_type,_typeDetail) {
                    print("_typeDetail: $_typeDetail");
                    List<TypeModel> typeModelList = [];
                    _typeDetail != null
                        ? _typeDetail.forEach((_typeName,_typeSubDetail) {
                            print("_typeSubDetail: $_typeSubDetail");
                            var typeModel = TypeModel(
                              typeName: _typeName.toString(),description:
                                  _typeSubDetail['description'].toString(),price: _typeSubDetail['price'].toString(),);
                            typeModelList.add(typeModel);
                          })
                        : null;
                    var priceModel = PriceModel(
                      type: _type,typeData: typeModelList,);
                    prices.add(priceModel);
                  })
                : null;
            var _eventModel = EventModel(
              ageLimit: _eventDetail['agelimit'].toString(),date: _eventDetail['date'].toString(),description: _eventDetail['description'].toString(),dressCode: _eventDetail['dresscode'].toString(),eventName: _event,image: _eventDetail['image'].toString(),lineup: _eventDetail['lineup'] as List<dynamic>,prices: prices,stars: double.parse(_eventDetail['stars']),time: _eventDetail['time'].toString(),);
            events.add(_eventModel);
          })
        : null;
    var _placeModel = PlaceModel(
      description: _details['description'],event: events,images: _details['images'],location: _details['location'],menu: _details['menu'],placeName: _placeName,stars: double.parse("${_details['stars']}"),logo: _details['logo'],);
    _places.add(_placeModel);
  });
}).then((_) {
  print("This is final: ${places[0].images[0]}");
  notifyListeners();
}).catchError((e) {
  print(e);
});

我得到的日志是:

I/Flutter (25748): Data: {Amethhyyst - Mumbai: {description: In the city that never sleeps,rises a luxury lifestyle lounge built with one sole vision - Live life. One experience at a time!,events: {Saturday club night: {agelimit: 18+,date: Sat 7,Nov 2020,description: It's Club Night and the place will be exploding with amazing music and your tapping on the floor. The party starts at 7 PM,so be there on 07th Nov and rejoice with Dj Manzee @amethhyyst!,dresscode: casuals,image: https://firebasestorage.googleapis.com/v0/b/guestinme-3aafb.appspot.com/o/places%2Famethhyyst%2Fevent%2F07112020_flyer.jpg?alt=media&token=967c8301-7730-40fb-98e4-1dfe07e7e5aa,lineup: [DJ Manzee],price: {crowd: {Couple: {description: 1 male and 1 female only,price: 0},Female: {description: 1 female only,Male Stags: {description: 1 male only,price: 2000}},tables: {Exclusive Table: {description: Exclusive table provided,price: 10000},VIP Table: {decription: VIP table provided,price: 20000}}},time: 19:00 - 01:30}},images: [http
I/Flutter (25748): Details: {description: In the city that never sleeps,images: [https://firebasestorage
I/Flutter (25748): _eventdetail: {agelimit: 18+,description: It's Club Night and the place will be exploding with amazing music 
and your tapping on the floor. The party starts at 7 PM,time: 19:00 - 01:30}
I/Flutter (25748): _typeDetail: {Couple: {description: 1 male and 1 female only,Male Stags: 
    {description: 1 male only,price: 2000}}
I/Flutter (25748): _typeSubDetail: {description: 1 male and 1 female only,price: 0}
I/Flutter (25748): _typeSubDetail: {description: 1 female only,price: 0}
I/Flutter (25748): _typeSubDetail: {description: 1 male only,price: 2000}
I/Flutter (25748): _typeDetail: {Exclusive Table: {description: Exclusive table provided,price: 20000}}
I/Flutter (25748): _typeSubDetail: {description: Exclusive table provided,price: 10000}
I/Flutter (25748): _typeSubDetail: {decription: VIP table provided,price: 20000}
I/Flutter (25748): NoSuchMethodError: The getter 'length' was called on null.
I/Flutter (25748): Receiver: null
I/Flutter (25748): Tried calling: length

我尝试了什么: 我看到了一些与此相关的帖子,并尝试通过初始化列表'[]'来删除null对象上的length方法调用。我还通过if条件检查它们是否确实为null,因此我可以跳过该部分而不运行它(:null;)。

我的Firebase数据:

This is my firebase database

解决方法

解决了。这是因为我在此处添加了“星星”,而数据库中没有“星星”键。

$headerFooterColor: #4b4276;
$headerFooterHeight: 50px;
$sidebarWidth: 15%;

* {
  margin: 0;
  padding: 0;
}
html,body {
  height: 100%;
  text-align: center;
}
#main {
  display: flex;
  flex-direction: column;
}

#header {
  font-size: 1rem;
  height: $headerFooterHeight;
  background-color: $headerFooterColor;
  text-align: center;
  color: white;
  width: 100%;
  left: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  height: $headerFooterHeight;
}

.wrapper {
  display: flex;
  flex: 1 0 0;
  margin-top: $headerFooterHeight;
  .sidebar {
    margin: 0;
    padding: 0;
    width: $sidebarWidth;
    background-color: #b9bcc9;
    position: fixed;
    height: calc(100% - (2 * #{$headerFooterHeight}));
    height: calc(100% - (2 * #{$headerFooterHeight}));
    height: calc(100% - (2 * #{$headerFooterHeight}));
    margin: 0 auto;
    color: white;
  }
  .sidebar-left {
    left: 0;
    order: -1;
  }

  .main {
    background-color: rgba(160,194,227,0.5);
    text-align: center;
    color: white;
    width: 70%;
    height: calc(100% - (2 * #{$headerFooterHeight}));
    overflow: auto;
    flex: 1 0 0;
    &.fullWidth {
      width: 100%;
    }
    &.leftSidebarEnabled {
      margin-left: 15%;
      width: 85%;
    }
    &.rightSidebarEnabled {
      margin-right: 15%;
      width: 85%;
    }
  }

  .sidebar-right {
    right: 0;
    order: 1;
  }
}

#footer {
  font-size: 1rem;
  position: fixed;
  width: 100%;
  bottom: 0;
  height: $headerFooterHeight;
  left: 0;
  background-color: $headerFooterColor;
  text-align: center;
  color: white;
  overflow: auto;
  z-index: 1;
}