NoSuchMethodError 颤动

发布于 2025-01-10 14:02:06 字数 2922 浏览 1 评论 0原文

请帮助我... 几天来我试图解决这个问题,但没有任何效果 TT

我不明白什么是 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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2025-01-17 14:02:06

这是你的问题:
您有一张名为 TTdata 的地图。这张地图开始没有任何价值。
然后 initstate 运行,调用 getTypeTemps
最后build运行。其中有这一行:

TTdata["TTdata"].map((i){

这一行是导致您的问题的原因,问题说“方法'[]'被调用为空”。意味着您在 null 变量(没有值的变量)之后添加了 ['something'] (如果您想了解 null 更好,我写了一篇关于null安全的文章,开头部分专门讲了null是什么意思)。

很明显,getTypeTemps 没有正确为 TTdata 分配值。这在读取时会导致错误。这是为什么呢?

原因很简单。 getTypeTemps 是一个异步方法,这意味着只要 flutter 有空闲时间,它就会执行,通常是在第一次运行构建之后。

这意味着您的代码按以下顺序执行:

初始化状态->构建->获取类型温度。

因此,如果 TTdata 仍然为空,您应该采取某种安全措施。
以下是您可以如何执行此操作的示例:

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: [
  ...
}

希望这个解释很清楚并可以帮助您解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文