-1

Hello I am making an api call where I am getting array of banners. I am getting below error -

_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>')

banners_bloc.dart

class BannersBloc {
  late BannerRepository _bannerRepository;
  late StreamController _streamController;

  StreamSink get bannerListSink => _streamController.sink;

  BannersBloc() {
    _streamController = StreamController();
    _bannerRepository = BannerRepository();
    fetchBanners();
  }

  fetchBanners() async {
    bannerListSink.add('Fetching Banners');
    try {
      Banners banners = await _bannerRepository.getBanners();
      bannerListSink.add(banners);
    } catch (e) {
      bannerListSink.add(e.toString());
      print(e);
    }
  }

  dispose() {
    _streamController.close();
  }
}

banners.dart

class Banners {
  late String bannerImageUrl;
  late String bannerImageAlt;
  late bool isActive;
  late int order;
  late String id;

  Banners(
      {required this.bannerImageUrl,
      required this.bannerImageAlt,
      required this.isActive,
      required this.order,
      required this.id});

  Banners.fromJson(Map<String, dynamic> json) {
    bannerImageUrl = json['bannerImageUrl'];
    bannerImageAlt = json['bannerImageAlt'];
    isActive = json['isActive'];
    order = json['order'];
    id = json['id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['bannerImageUrl'] = this.bannerImageUrl;
    data['bannerImageAlt'] = this.bannerImageAlt;
    data['isActive'] = this.isActive;
    data['order'] = this.order;
    data['id'] = this.id;
    return data;
  }
}

api_base_helper.dart

class APIHelper {
  final String _baseURL =
      "https://raw.githubusercontent.com/gautam-in/shopping-cart-assignment/master/server/";

  Future<dynamic> get(String url) async {
    try {
      final response = await http.get(Uri.parse(_baseURL + url));
      return json.decode(response.body);
    } catch (err) {
      throw new Exception("Something went wrong");
    }
  }
}

get_banners.dart

class BannerRepository {
  Future getBanners() async {
    final response = await new APIHelper().get("banners/index.get.json");
    return Banners.fromJson(response);
  }
}
1

1 Answer 1

2

If your response from the API is a List of Banners, then the error is being thrown by getBanners. As the fromJson method is expecting a of type Map<String, dynamic>. To solve this you'll need to iterate through the response and call Banners.fromJson for each item in the response list.

class BannerRepository {
  Future<List<Banners>> getBanners() async {
    final response = await new APIHelper().get("banners/index.get.json");
   // Sample Code, handler errors and other stuff
    return response.map((e) =>Banners.fromJson(e)).toList();
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Where is your error arising from? Paste the full error, so we can track down the error correctly
this line return response.map((e) => Banners.fromJson(response)).toList(); flutter: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
My Mistake, it should be e not response. Updated the code block
after iterating this map now, going flow to pojo class in the fromJson, then after iteration it is giving same error.
Update your question, Include the exact line number and the file where the error occurs.
|

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.