3

I'm trying to get the JSON response from the server and send it to the console but it's returning the following error.

enter image description here

Class BannerApi

class BannerApi {
  String id;
  String nome;
  String bclass;
  List<BannerItemApi>? bannerItem;

  BannerApi({
    required this.id,
    required this.nome,
    required this.bclass,
    required this.bannerItem,
  });

  factory BannerApi.fromJson(Map<String, dynamic> json) {
    inspect(json);
    return BannerApi(
      id: json['id'].toString(),
      nome: json['name'],
      bclass: json['class'],
      bannerItem: json['activeBannerItems'].length == 0
          ? null
          : List<BannerItemApi>.from(json['activeBannerItems'].forEach((x) {
              BannerItemApi.fromJson(x);
            })),
    );
  }
}

Class BannerItemApi

class BannerItemApi {
  String id;
  String titulo;
  String descricao;
  String imageUrl;

  BannerItemApi({
    required this.id,
    required this.titulo,
    required this.descricao,
    required this.imageUrl,
  });

  factory BannerItemApi.fromJson(Map<String, dynamic> json) {
    inspect(json);
    return BannerItemApi(
      id: json['item'][0]['id'].toString(),
      titulo: json['item'][0]['title'],
      descricao: json['item'][0]['text'],
      imageUrl: json['item'][0]['image'],
    );
  }
}

Server response does not return null at any time.

Inspect from BannerItemApi.fromJson(x)

enter image description here

11
  • Can you share your model as well ? Commented Dec 30, 2021 at 17:15
  • ok, i already edited the question Commented Dec 30, 2021 at 17:17
  • The model is incomplete still there should be model of BannerItemApi also Commented Dec 30, 2021 at 17:18
  • Sorry, I already put Commented Dec 30, 2021 at 17:22
  • 2
    Basically this happens when you are getting a null value from response and the variable that you are assign value is not nullable. If you still have doubt then please share a sample of json response that you are getting from server then I'll let you know exact reason. Commented Dec 30, 2021 at 18:01

2 Answers 2

3

Change

json['activeBannerItems'].length == 0
          ? null
          : List<BannerItemApi>.from(json['activeBannerItems'].forEach((x) {
              BannerItemApi.fromJson(x);
            })),

to

json['activeBannerItems'] == null
          ? null
          : (json['activeBannerItems'] as List)
              .map((i) => BannerItemApi.fromJson(i))
              .toList(),
Sign up to request clarification or add additional context in comments.

Comments

0
bannerItem: json['activeBannerItems'].length == 0
          ? null:.. 

replace for

bannerItem: json['activeBannerItems'].length == 0
              ? [] ...

in bannerapi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.