Help me please... I tried to solve this problem since some days but nothing worked T.T
I have a DB phpmyadmin
I want to show a list from json delivered by a php url. I tried to adapt this code, https://www.fluttercampus.com/guide/53/how-to-make-drop-down-and-insert-options-by-php-mysql-in-flutter/ but I have this error
The following NoSuchMethodError was thrown building AddTimePage(dirty, dependencies: [MediaQuery], state: AddTimeState#1a618): The method '[]' was called on null. Receiver: null Tried calling:
I don't understand what is null or [] ... The json I received is OK
My function to get data
Future<dynamic> getTypeTemps() async {
var res = await http.post(Uri.parse(TTurl "?action=LST"));
if (res.statusCode == 200) {
setState(() {
TTdata = json.decode(res.body);
});
} else {
setState(() {error = true; message = "Erreur avec la table TypeTemps";});
} }
getTypeTemps is called in void initstate()
The widget to show the list
Widget WdgListTT() {
List<TypesTemps> ttlist = List<TypesTemps>.from(
TTdata["TTdata"].map((i){
return TypesTemps.fromJSON(i);
})
); //searilize typetemps json data to object model.
return Column(children: [
const Padding(
padding: EdgeInsets.only(bottom: 20),
),
SizedBox(
width: MediaQuery.of(context).size.width - 40,
child: const Text("Type de temps", style: TextStyle(color: Colors.teal, fontWeight: FontWeight.bold, fontSize: 18),),
),
SizedBox(
width: MediaQuery.of(context).size.width - 50,
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
//style: const TextStyle(color: Colors.white),
value: valtypetemps,
onChanged: (value) => setState(() => valtypetemps = value),
elevation: 20,
underline: Container(
height: 10,
color: Colors.red,
),
hint: const Text("Sélectionnez un type",
style: TextStyle(fontSize: 16)),
isExpanded: true,
items: ttlist.map((typesTemps) {
return DropdownMenuItem(
child: Text(typesTemps.libelle,
style: TextStyle(color: Colors.black)),
value: typesTemps.libelle,
);
}).toList(),
)))
]); }
The class
class TypesTemps { String code, libelle, type_temps; TypesTemps({
required this.code,
required this.libelle,
required this.type_temps, }); factory TypesTemps.fromJSON(Map json) {
return TypesTemps(
code: json["CODE"],
libelle: json["LIBELLE"],
type_temps: json["SENS_TYPE_TEMPS"]); } }
CodePudding user response:
Here is your problem:
You have a certain map called TTdata
. This map starts having no value.
Then initstate runs, which calls getTypeTemps
.
Finally build
runs. Which has this line in it:
TTdata["TTdata"].map((i){
This line is what is causing your issue, the issue says "The method '[]' was called on null". Meaning you added a ['something']
after a null variable (a variable with no value) (If you want to understand what null is better, I wrote an article on null safety, the beginning part specially talks about what null
means).
So clearly, getTypeTemps
is not properly assigning a value to TTdata
. which is causing an error when reading from it. Why is this?
The reason is simple. getTypeTemps
is an asynchronous method, meaning it will execute whenever flutter has some free time, usually after running build for the first time.
That means your code executes in this order:
initState -> build -> getTypeTemps.
So you should have some sort of safety meassure if TTdata
is still null.
Here is an example of how you might do this:
Widget build(BuildContext context) {
if (TTdata == null) {
return Text('loading...');
}
List<TypesTemps> ttlist = List<TypesTemps>.from(
TTdata["TTdata"].map((i){
return TypesTemps.fromJSON(i);
})
); //searilize typetemps json data to object model.
return Column(children: [
...
}
Hopefully this explanaition is clear and helps you solve your issue.