Home > other >  type 'List<dynamic>' is not a subtype of type 'Map<String, Widget>'
type 'List<dynamic>' is not a subtype of type 'Map<String, Widget>'

Time:08-28

'class CarouselImages extends StatefulWidget {
  const CarouselImages({
    Key? key,
  }) : super(key: key);

  @override
  State<CarouselImages> createState() => _CarouselImagesState();
}

class _CarouselImagesState extends State<CarouselImages> {
  Advertise? advertises;
  final AdvertiseService advertiseService = AdvertiseService();

  @override
  void initState() {
    super.initState();
    fetchAdsOfDay();
  }

  fetchAdsOfDay() async {
    advertises = (await advertiseService.fetchAdsOfDay(context: context));
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return advertises == null
        ? const Loader()
        : Container(
            margin: EdgeInsets.symmetric(vertical: 12),
            height: 200,
            child: Swiper(
              autoplay: true,
              autoplayDelay: 1500,
              curve: Curves.easeIn,
              itemCount: advertises!.images.length,
              itemBuilder: (BuildContext context, int index) {
                final advertiseData = advertises!.images;
                return ClipRRect(
                    borderRadius: BorderRadius.circular(10),
                    child: Image(
                      image: NetworkImage(advertiseData[0]),
                      fit: BoxFit.fitHeight,
                    ));
              },
              viewportFraction: 0.8,
              scale: 0.9,
              pagination: SwiperPagination(),
            ),
          );
  }
}

'

// type 'List' is not a subtype of type 'Map<String, Widget>' showing this error whenever i am trying to retrive image from database this is showing what to do now''

my model of the question

// ignore_for_file: public_member_api_docs, sort_constructors_first import 'dart:convert';

class Advertise {
  final String name;
  final String description;
  final List<String> images;
  final double price;
  final String? id;

  Advertise({
    this.id,
    required this.name,
    required this.description,
    required this.images,
    required this.price,
  });

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'name': name,
      'description': description,
      'images': images,
      'price': price,
      'id': id,
    };
  }

  factory Advertise.fromMap(Map<String, dynamic> map) {
    return Advertise(
      name: map['name'] as String,
      description: map['description'] as String,
      images: List<String>.from(
        (map['images']),
      ),
      price: map['price'] as double,
      id: map['id'] != null ? map['id'] as String : null,
    );
  }

  String toJson() => json.encode(toMap());

  factory Advertise.fromJson(String source) =>
      Advertise.fromMap(json.decode(source));
}

// thats it thanks in advance for replying qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

CodePudding user response:

In Advertise.fromMap you expect Map<String, dynamic> map but what you are giving it is a List so change Advertise model fromMap to fromList, like this:

static List<Advertise> fromList(List _list) {
    List<Advertise> result = [];

    for (var item in _list) {
      var advertise = Advertise(
         name: item['name'] as String,
         description: item['description'] as String,
         images: List<String>.from((item['images']),),
         price: item['price'] as double,
         id: item['id'] != null ? item['id'] as String : null,
      );
      result.add(advertise);
    }
    return result;
  }
  • Related