Mapping image data to the list .List<T>', is a potentially non-nullable type?

Viewed 28

I am trying to upload image from the Assets folder image but getting an error while trying to map the list of image. I have created CarouselModel and trying to map. Error as The body might complete normally, causing 'null' to be returned, but the return type, 'List<T>', is a potentially non-nullable type. How can It be solved? Thank you in advance.

class model :

class CarouselModel {
  String image;
  CarouselModel(this.image);    }

List<CarouselModel> carsouels =
    carsouelsData.map((item) => CarouselModel(item['image']!)).toList();    
var carsouelsData = [
  {"image": "images/pic1.jpg"},
  {"image": "images/pic2.png"},
  {"image": "images/pic3.jpg"},
];

Trying to map using:

List<T> map<T>(List list, Function handler) {  
    List<T> result = [];
    for (var i = 0; i < list.length; i++) {
      result.add(handler(i, list[i]));      
    }
  }

View code:

  body: Container(
    child: ListView(
      physics: ClampingScrollPhysics(),
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(left: 16, right: 16),
          child: Text(
            'welcome to the beautiful world !',
            style: mTitleStyle,
          ),
        ),
        Container(
          alignment: Alignment.centerLeft,
          margin: EdgeInsets.only(left: 16, right: 16),
          width: MediaQuery.of(context).size.width,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                width: MediaQuery.of(context).size.width,
                height: 190,
                child: Swiper(
                  onIndexChanged: (index) {
                    setState(() {
                      _current = index;
                    });
                  },
                  autoplay: true,
                  layout: SwiperLayout.DEFAULT,
                  itemCount: carsouels.length,
                  itemBuilder: (BuildContext context, index) {
                    return Container(
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8),
                        image: DecorationImage(
                          image: AssetImage(
                            carsouels[index].image,
                          ),
                          fit: BoxFit.cover,
                        ),
                      ),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      ],
    ),
  ),
2 Answers
List<T> map<T>(List list, Function handler) {  
    List<T> result = [];
    for (var i = 0; i < list.length; i++) {
      result.add(handler(i, list[i]));      
    }
// TODO: return result;
  }

Here, you need to return result.

However, what's the point of this function? List already has a built-in map method.

It seems to me that you need to return the result from map.

List<T> map<T>(List list, Function handler) {  
    List<T> result = [];
    for (var i = 0; i < list.length; i++) {
       result.add(handler(i, list[i]));      
    }
    return result;
}
Related