【问题标题】:Flutter: NoSuchMethodError: The getter 'length' was called on nullFlutter:NoSuchMethodError:在 null 上调用了 getter 'length'
【发布时间】:2021-02-23 18:57:53
【问题描述】:

我正在尝试从我的 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, price: 0}, 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, 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, price: 0}, 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: [https://firebasestorage
I/flutter (25748): _eventdetail: {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, price: 0}, 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}
I/flutter (25748): _typeDetail: {Couple: {description: 1 male and 1 female only, price: 0}, Female: {description: 1 female only, price: 0}, 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: 10000}, VIP Table: {decription: VIP 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

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

我的 Firebase 数据:

【问题讨论】:

    标签: firebase flutter httprequest flutter-listview flutter-http


    【解决方案1】:

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

    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'],
        );
    

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 2021-08-11
      • 2021-12-10
      • 2018-12-03
      • 2020-06-15
      • 2021-05-25
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多