-1

I am trying to get some data from my firebase realtime database.This is my code in order to get the data through http request. I am receiving the data as well and can see it in the logs.

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);
});

The logs that I am getting are:

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

What I tried: I saw some posts regarding this and tried to remove the length method call on null objects by initializing the lists '[]'. I also made the if condition of checking whether they are indeed null so I can skip that part and not run it (: null;).

My firebase data:

This is my firebase database

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Ali Solanki
  • 640
  • 7
  • 16

1 Answers1

0

Solved it. It is because I added "stars" here whereas there is no "stars" key in the database.

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'],
    );
Ali Solanki
  • 640
  • 7
  • 16