0
votes
Map<String, dynamic> toJson() => {
    "username": username,
    "Event_date_time": eventDateTime,
    "Event_name": eventName,
    "Event_Address": eventAddress,
    "Utensils": utensils,
    "Hot_serve": hotServe,
    "Carting": carting,
    "Beverage ": List<dynamic>.from(Beverage.map((x) => x.toJson())),

  };

class Beverage {
  String beveragesitem;
  String beveragesCount;

  Beverage({
    this.beveragesitem,
    this.beveragesCount,
  });

  factory Beverage.fromJson(Map<String, dynamic> json) => Beverage(
    beveragesitem: json["Beveragesitem"],
    beveragesCount: json["Beverages_count"],
  );

  Map<String, dynamic> toJson() => {
    "Beveragesitem": beveragesitem,
    "Beverages_count": beveragesCount,
  };
}

Error showing like below

Unhandled Exception: NoSuchMethodError: The getter 'length' was called on null. E/flutter ( 6308): Receiver: null E/flutter ( 6308): Tried calling: length E/flutter ( 6308): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) E/flutter ( 6308): #1 _Uri._uriEncode (dart:core-patch/uri_patch.dart:44:23) E/flutter ( 6308): #2 Uri.encodeQueryComponent (dart:core/uri.dart:1103:17) E/flutter ( 6308): #3 mapToQuery. (package:http/src/utils.dart:19:13) E/flutter ( 6308): #4 CastMap.forEach. (dart:_internal/cast.dart:286:8) E/flutter ( 6308): #5 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)

Can anyone help correcting this issue?

2

2 Answers

0
votes

Where are you calling toJson()maybe there is the problem?

0
votes

I have found the issue, may be this will help someone, I was trying to call an Api here with random values for testing purpose. some values were empty while I was testing, when the model class doesnot find value in the structure throwing error as 'length' was called on null, i.e, no value entered for that model variable. When I put all values for the model class it returned success

In short - while calling toJson method, no values should be null