i am getting this error Null check operator used on a null value ? from the line imageList! how to solve this thank you in advance using late modifier bring another error LateInitializationError: Field 'imageList' has not been initialized.
class SliderView extends StatefulWidget {
const SliderView({Key? key}) : super(key: key);
@override
State<SliderView> createState() => _SliderViewState();
}
class _SliderViewState extends State<SliderView> {
List? imageList;
bool loading = true;
String imageFolder = "http://192.168.1.72/flutterverify/upload/";
fetchSlider() async {
final response = await http
.get(Uri.parse("http://192.168.1.72/flutterverify/slider.php"));
if (response.statusCode == 200) {
setState(() {
imageList = jsonDecode(response.body);
loading = false;
});
}
print(imageList);
}
@override
void initState() {
imageList;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("SLider"),
),
body: Container(
child: Carousel(
boxFit: BoxFit.cover,
autoplay: false,
animationCurve: Curves.fastOutSlowIn,
animationDuration: Duration(milliseconds: 1000),
dotSize: 6.0,
dotIncreasedColor: Color(0xFFFF335C),
dotBgColor: Colors.transparent,
dotPosition: DotPosition.topRight,
dotVerticalPadding: 10.0,
showIndicator: true,
indicatorBgPadding: 7.0,
images: imageList!.map((element) {
return new Image.network(imageFolder element['image']);
}).toList()),
),
);
}
} ```
CodePudding user response:
It is a null safety error, The imageList
is set from a http.get which can be null, so replace this:
if (response.statusCode == 200) {
setState(() {
imageList = jsonDecode(response.body);
loading = false;
});
}
By this
if (response.statusCode == 200) {
setState(() {
imageList = jsonDecode(response.body ?? '[]');
loading = false;
});
}
Doing this you affect by default an empty array to imageList
in case of null value.
CodePudding user response:
You need to change rows that i mentioned below:
print(imageList); => print(imageList!);
and this block
@override
void initState() {
imageList; => imageList!;
super.initState();
}