英文:
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 '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));
});
}
}
}
答案1
得分: 8
List<CardResults>()
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 <CardResults>[]
. But personally, I prefer creating the completed list at the declaration, rather than creating an empty list and building it up.
英文:
List<CardResults>()
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 <CardResults>[]
. 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()
。相反,您可以使用 <CardResults>[]
或 List<CardResults>.empty()
来创建一个空列表。
有关 List
类型的可用构造函数的详细信息,请参阅这里。
其次,您可以优化您的代码,而不是创建一个空列表,然后为每个项目调用 add
方法,您可以使用 map
方法,例如:
CardModel.fromJson(Map<String, dynamic> json) {
if (json['cardResults'] != null) {
results = json['cardResults'].map<CardResults>((v) => CardResults.fromJson(v)).toList();
}
}
有关 map
方法的详细信息,请参阅这里。
第三,您可以将属性标记为 late
,也可以将其标记为 final
并使用 factory constructor
来实例化对象。最后,您的 CardModel
应该如下所示:
class CardModel {
final List<CardResults> results;
CardModel({required this.results});
factory CardModel.fromJson(Map<String, dynamic> json) {
return CardModel(
results: json['cardResults']?.map<CardResults>((v) => 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 <CardResults>[]
or List<CardResults>.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<String, dynamic> json) {
if (json['cardResults'] != null) {
results = json['cardResults'].map<CardResults>((v) => 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<CardResults> results;
CardModel({required this.results});
factory CardModel.fromJson(Map<String, dynamic> json) {
return CardModel(
results: json['cardResults']?.map<CardResults>((v) => 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<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)));
}
// return an empty list if cardResults is empty
return CardModel(results: []);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论