I have json response from API and I want to display it in a carouselslider. here is my response from API
[
{
"header": "Header 1",
"subheader": "",
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet vestibulum condimentum. Nunc finibus porttitor purus, in volutpat ante luctus in.",
"imageurl": "https://www.bannerbatterien.com/upload/filecache/Banner-Batterien-Windrder2-web_06b2d8d686e91925353ddf153da5d939.webp",
"footer": "Footer 1",
"releasedate": "01 Aug 2019",
"bodyimage": ""
},
{
"header": "Header 2",
"subheader": "",
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet vestibulum condimentum. Nunc finibus porttitor purus, in volutpat ante luctus in.",
"imageurl": "https://i.pinimg.com/originals/0b/a3/d6/0ba3d60362c7e6d256cfc1f37156bad9.jpg",
"footer": "Footer 2",
"releasedate": "06 Sep 2019",
"bodyimage": ""
}
]
I have declare List<NewsPromotion> imgSlider;
and when I got my response from API, I'm trying to insert it into my model imgSlider = menuModel[3].news_promotion;
and try to call it
class NewsPromotion {
String header;
String subheader;
String content;
String imageurl;
String footer;
String releasedate;
String bodyimage;
NewsPromotion(
this.header,
this.subheader,
this.content,
this.imageurl,
this.footer,
this.releasedate,
this.bodyimage,
);
factory NewsPromotion.fromJson(dynamic json) {
return NewsPromotion(
json['header'] as String,
json['subheader'] as String,
json['content'] as String,
json['imageurl'] as String,
json['footer'] as String,
json['releasedate'] as String,
json['bodyimage'] as String,
);
}
@override
String toString() {
return '{ ${this.header} ,${this.subheader} ,${this.content} ,${this.imageurl} ,${this.footer} ,${this.releasedate} ,${this.bodyimage}}';
}
}
final CarouselSlider autoPlayImage = CarouselSlider(
options: CarouselOptions(
height: 180.0,
autoPlay: true,
enlargeCenterPage: true,
aspectRatio: 16 / 9,
),
items: imgSlider.map((fileImage) {
return Builder(
builder: (BuildContext context) {
return Container(
margin: EdgeInsets.all(5.0),
child: GestureDetector(
child: Image.asset(
'assets/${fileImage}',
width: 10000,
fit: BoxFit.cover,
),
onTap: () {
//
},
),
);
},
);
}).toList(),
);
But it give me errror
The instance member 'imgSlider' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression