类 ‘List’ 没有无名构造函数

huangapple go评论61阅读模式
英文:

The class 'List' doesn't have an unnamed constructor

问题

I am trying to write a model but i keep getting this error. Here is the code.

import 'package:flutter/material.dart';

class CardModel {
  late List<CardResults> results;

  CardModel({ required this.results});

  CardModel.fromJson(Map<String, dynamic> json) {
    if (json['cardResults'] != null) {
      results = List<CardResults>();
      json['cardResults'].forEach((v) {
        results.add(CardResults.fromJson(v));
      });
    }
  }
}

我正在尝试编写一个模型,但一直出现这个错误。以下是代码。

英文:

I am trying to write a model but i keep getting this error. Here is the code.

import &#39;package:flutter/material.dart&#39;;

class CardModel {
late List&lt;CardResults&gt; results;

CardModel({ required this.results});

CardModel.fromJson(Map&lt;String, dynamic&gt; json) {
if (json[&#39;cardResults&#39;] != null) {
  results = List&lt;CardResults&gt;();
  json[&#39;cardResults&#39;].forEach((v) {
    results.add(CardResults.fromJson(v));
  });
  }
 }
}

答案1

得分: 8

List&lt;CardResults&gt;() is no longer valid Dart code as of Dart 3.0.
See https://github.com/dart-lang/sdk/issues/49529 for details.

You can instead use &lt;CardResults&gt;[]. But personally, I prefer creating the completed list at the declaration, rather than creating an empty list and building it up.

英文:

List&lt;CardResults&gt;() is no longer valid Dart code as of Dart 3.0.
See https://github.com/dart-lang/sdk/issues/49529 for details.

You can instead use &lt;CardResults&gt;[]. But personally, I prefer creating the completed list at the declaration, rather than creating an empty list and building it up.

答案2

得分: 3

首先,List 类型没有默认构造函数,因此无法使用 List()。相反,您可以使用 &lt;CardResults&gt;[]List&lt;CardResults&gt;.empty() 来创建一个空列表。

有关 List 类型的可用构造函数的详细信息,请参阅这里


其次,您可以优化您的代码,而不是创建一个空列表,然后为每个项目调用 add 方法,您可以使用 map 方法,例如:

CardModel.fromJson(Map&lt;String, dynamic&gt; json) {
  if (json[&#39;cardResults&#39;] != null) {
    results = json[&#39;cardResults&#39;].map&lt;CardResults&gt;((v) =&gt; CardResults.fromJson(v)).toList();
  }
}

有关 map 方法的详细信息,请参阅这里


第三,您可以将属性标记为 late,也可以将其标记为 final 并使用 factory constructor 来实例化对象。最后,您的 CardModel 应该如下所示:

class CardModel {
  final List&lt;CardResults&gt; results;

  CardModel({required this.results});

  factory CardModel.fromJson(Map&lt;String, dynamic&gt; json) {
    return CardModel(
      results: json[&#39;cardResults&#39;]?.map&lt;CardResults&gt;((v) =&gt; CardResults.fromJson(v)).toList() ?? [],
    );
  }
}

您可以在这里了解更多关于工厂构造函数的信息。

英文:

First, List type doesn't have a default constructor so doing List() is not possible. So, instead you can create an empty list using &lt;CardResults&gt;[] or List&lt;CardResults&gt;.empty().

You can learn more about available constructors for List type here.


Second, you can optimize your code and instead of creating an empty list and calling add for each item you can use map method, like:

CardModel.fromJson(Map&lt;String, dynamic&gt; json) {
  if (json[&#39;cardResults&#39;] != null) {
    results = json[&#39;cardResults&#39;].map&lt;CardResults&gt;((v) =&gt; CardResults.fromJson(v)).toList();
  }
}

You can learn more about map method here.


Third, instead of marking a property as late you can make it final and use factory constructor to instantiate an object. Finally, your CardModel should look like this:

class CardModel {
  final List&lt;CardResults&gt; results;

  CardModel({required this.results});

  factory CardModel.fromJson(Map&lt;String, dynamic&gt; json) {
    return CardModel(
      results: json[&#39;cardResults&#39;]?.map&lt;CardResults&gt;((v) =&gt; CardResults.fromJson(v)).toList() ?? [],
    );
  }
}

You can learn more about factory constructors here.

答案3

得分: 1

以下是您要翻译的代码部分:

class CardModel {
  // 假设您的列表是字符串列表
  List<String> results;

  CardModel({required this.results});

  factory CardModel.fromJson(Map<String, dynamic> json) {
    if (json['cardResults'] != null) {
      return CardModel(
          results: List<String>.from(json["cardResults"].map((x) => x)));
    }
    // 如果cardResults为空,则返回一个空列表
    return CardModel(results: []);
  }
}
英文:

Since you are getting the list of results from a JSON, you can map get the model list like this:

class CardModel {
//assuming your list is a list of strings
List&lt;String&gt; results;

CardModel({required this.results});

factory CardModel.fromJson(Map&lt;String, dynamic&gt; json) {
  if (json[&#39;cardResults&#39;] != null) {
  return CardModel(
      results: List&lt;String&gt;.from(json[&quot;cardResults&quot;].map((x) =&gt; x)));
 }
 // return an empty list if cardResults is empty
 return CardModel(results: []);
 }
} 

huangapple
  • 本文由 发表于 2023年6月8日 07:38:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定