I am fetching data from an api and showing in ListView builder But its showing error
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
bool show = false;
var map = {};
@override
void initState() {
dosomestuff();
}
Future<List> dosomestuff() async {
http.Response res = await http.get(
'http://retailapi.airtechsolutions.pk/api/menu/2112',
);
Map<String, dynamic> map = json.decode(res.body);
if (map['description'] == "Success") {
print('show kr ');
print(map["Categories"].length);
print(map["Categories"]);
setState(() {
show = true;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: show
? ListView.builder(
itemCount: map["Categories"].length,
scrollDirection: Axis.horizontal,
shrinkWrap: true,
physics: BouncingScrollPhysics(),
itemBuilder: (context, index) {
return Container(
width: 80.0,
child: Text(map["Categories"][index]['Name'])
);
},
) : Container(),
);
}
}
Its showing error The method '[]' was called on null
. But when i print the values in state function the its priting values and all things are working but when ill show in ListBuilder then its showing that error.
You can see i print the values in function
print(map["Categories"].length);
print(map["Categories"]);
And its printing values all things fine.
You can see in image its printing the length and arrays fine so its not null when i use in widget its saying length is called on null -_-
question from:
https://stackoverflow.com/questions/65830268/flutter-showing-the-method-was-called-on-null-on-listbuilder 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…