I am trying to add data from the database (MYSQL database) as json data. I received this error, so I tried to print the data that came from (MYSQL) in the console and it appeared and it did not contain an error, but when I try to add it to the drop-down list, this error appears. Can anyone Help me out
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:youtubeclone/pages/class/categores.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
//CALLING STATE API HERE
// Get State information by API
List statesList;
String _myState;
Future<String> _getStateList() async {
String url = 'http://10.0.2.2/videoTube/chosecategories.php';
var response = await http.get(url);
var responsebody = json.decode(response.body);
//print(data);
setState(() {
if (responsebody != null) {
for (int i = 0; i < responsebody.length; i++) {
print(responsebody[i]['name']);
var name = responsebody[i]['name'];
var cateygory_list = CategoresList(name: name);
statesList.add(cateygory_list);
}
}
});
}
// Get State information by API
@override
void initState() {
_getStateList();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dynamic DropDownList REST API'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
alignment: Alignment.topCenter,
margin: EdgeInsets.only(bottom: 100, top: 100),
child: Text(
'KDTechs',
style: TextStyle(fontWeight: FontWeight.w800, fontSize: 20),
),
),
//======================================================== State
Container(
padding: EdgeInsets.only(left: 15, right: 15, top: 5),
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
value: _myState,
iconSize: 30,
icon: (null),
style: TextStyle(
color: Colors.black54,
fontSize: 16,
),
hint: Text('Select State'),
onChanged: (String newValue) {
setState(() {
_myState = newValue;
//_getCitiesList();
print(_myState);
});
},
items: statesList?.map((item) {
return new DropdownMenuItem(
child: new Text(item['name']),
value: item['id'].toString(),
);
})?.toList() ??
[],
),
),
),
),
],
),
),
SizedBox(
height: 30,
),
],
),
);
}
}
this is class CategoresList
import 'package:flutter/material.dart';
class CategoresList {
final name;
CategoresList({this.name});
}