NoSuchMethodError 颤动
请帮助我... 几天来我试图解决这个问题,但没有任何效果 TT
我有一个数据库 phpmyadmin
我想显示由 php url 传递的 json 列表。 我尝试改编此代码, https://www.fluttercampus.com/guide/53/how-to-make-drop-down-and-insert-options-by-php-mysql-in-flutter/ 但我有这个错误
构建 AddTimePage(dirty, dependency: [MediaQuery], state: AddTimeState#1a618) 时抛出以下 NoSuchMethodError:在 null 上调用方法“[]”。接收者:null 尝试调用:
我不明白什么是 null 或 [] ... 我收到的 json 没问题
我获取数据的函数
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 在 void initstate() 中调用
显示列表的小
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(),
)))
]); }
部件 类
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"]); } }
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 errorThe 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"]); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是你的问题:
您有一张名为
TTdata
的地图。这张地图开始没有任何价值。然后 initstate 运行,调用
getTypeTemps
。最后
build
运行。其中有这一行:这一行是导致您的问题的原因,问题说“方法'[]'被调用为空”。意味着您在 null 变量(没有值的变量)之后添加了
['something']
(如果您想了解 null 更好,我写了一篇关于null安全的文章,开头部分专门讲了null
是什么意思)。很明显,
getTypeTemps
没有正确为TTdata
分配值。这在读取时会导致错误。这是为什么呢?原因很简单。
getTypeTemps
是一个异步方法,这意味着只要 flutter 有空闲时间,它就会执行,通常是在第一次运行构建之后。这意味着您的代码按以下顺序执行:
因此,如果 TTdata 仍然为空,您应该采取某种安全措施。
以下是您可以如何执行此操作的示例:
希望这个解释很清楚并可以帮助您解决问题。
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: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 whatnull
means).So clearly,
getTypeTemps
is not properly assigning a value toTTdata
. 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:
So you should have some sort of safety meassure if
TTdata
is still null.Here is an example of how you might do this:
Hopefully this explanaition is clear and helps you solve your issue.