NoSuchMethodError (NoSuchMethodError: 方法 'map' 在 null 上被调用
Posted
技术标签:
【中文标题】NoSuchMethodError (NoSuchMethodError: 方法 \'map\' 在 null 上被调用【英文标题】:NoSuchMethodError (NoSuchMethodError: The method 'map' was called on nullNoSuchMethodError (NoSuchMethodError: 方法 'map' 在 null 上被调用 【发布时间】:2021-10-25 06:19:51 【问题描述】:获取章节列表时发生异常。 那么我该如何解决这个问题呢? 请帮忙。
这是我的 API 响应。
"success": 1,
"chapter": [
"chapter_id": "609cb13f497e3",
"chapter_name": "test",
"subject_id": "5e32874c714fa",
"medium_id": "5d15938aa1344",
"standard_id": "5d1594e283e1a",
"material": null,
"textbook": null,
"test_paper": null,
"test_paper_solution": null,
"subject_memory_map": null,
"active": "1"
]
我在 chapter_model.dart 文件中创建的模型类。
// To parse this JSON data, do
//
// final chapterBySubjectModel = chapterBySubjectModelFromJson(jsonString);
import 'dart:convert';
ChapterBySubjectModel chapterBySubjectModelFromJson(String str) => ChapterBySubjectModel.fromJson(json.decode(str));
String chapterBySubjectModelToJson(ChapterBySubjectModel data) => json.encode(data.toJson());
class ChapterBySubjectModel
ChapterBySubjectModel(
required this.success,
required this.chapter,
);
int success;
List<Chapter> chapter;
factory ChapterBySubjectModel.fromJson(Map<String, dynamic> json) => ChapterBySubjectModel(
success: json["success"],
chapter: List<Chapter>.from(json["chapter"].map((x) => Chapter.fromJson(x))),
);
Map<String, dynamic> toJson() =>
"success": success,
"chapter": List<dynamic>.from(chapter.map((x) => x.toJson())),
;
class Chapter
Chapter(
required this.chapterId,
required this.chapterName,
required this.subjectId,
required this.mediumId,
required this.standardId,
this.material,
this.textbook,
this.testPaper,
this.testPaperSolution,
this.subjectMemoryMap,
required this.active,
);
String chapterId;
String chapterName;
String subjectId;
String mediumId;
String standardId;
dynamic material;
dynamic textbook;
dynamic testPaper;
dynamic testPaperSolution;
dynamic subjectMemoryMap;
String active;
factory Chapter.fromJson(Map<String, dynamic> json) => Chapter(
chapterId: json["chapter_id"],
chapterName: json["chapter_name"],
subjectId: json["subject_id"],
mediumId: json["medium_id"],
standardId: json["standard_id"],
material: json["material"],
textbook: json["textbook"],
testPaper: json["test_paper"],
testPaperSolution: json["test_paper_solution"],
subjectMemoryMap: json["subject_memory_map"],
active: json["active"],
);
Map<String, dynamic> toJson() =>
"chapter_id": chapterId,
"chapter_name": chapterName,
"subject_id": subjectId,
"medium_id": mediumId,
"standard_id": standardId,
"material": material,
"textbook": textbook,
"test_paper": testPaper,
"test_paper_solution": testPaperSolution,
"subject_memory_map": subjectMemoryMap,
"active": active,
;
我在 api_manager.dart 文件中创建的方法。
Future<List<Chapter>> getChapterBySubject() async
final chapterUrl =
'$baseUrl/subject/get_by_user_plan?user_id=609cab2cd5b6c&order_id=1620889722609cd07a601af469889697609cab2cd5b6c&standard_id=5d1594e283e1a&medium_id=5d15938aa1344';
final response = await http.get(Uri.parse(chapterUrl));
if (response.statusCode == 200)
final chapterData = chapterBySubjectModelFromJson(response.body);
final List<Chapter> chapters = chapterData.chapter;
print(chapters);
return chapters;
else
return <Chapter>[];
并在 chapter_widget.dart 文件中查看如下。
class _ChapterWidgetState extends State<ChapterWidget>
late bool _loading;
var _chapters = <Chapter>[];
@override
void initState()
super.initState();
_loading = true;
ApiManager().getChapterBySubject().then((chapters)
setState(()
_chapters = chapters;
_loading = false;
);
);
@override
Widget build(BuildContext context)
return ListView.builder(
itemCount: null == _chapters ? 0 : _chapters.length,
//itemCount: _chapters.length,
itemBuilder: (context, index)
Chapter chapter = _chapters[index];
return Container(
padding: EdgeInsets.all(8),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: InkWell(
//child: Image.asset("assets/logos/listbackground.png"),
child: Text(chapter.chapterName),
),
),
),
);
);
它在下一行的模型类中引发异常。
List<Chapter>.from(json["chapter"].map((x) => Chapter.fromJson(x))),
【问题讨论】:
查看***.com/a/52199176/2804581 不,它对我不起作用。它抛出相同的异常。 【参考方案1】:您将chapter
设置为required
,但似乎API 说它可以是null
。因此,您应该将参数从 required
转换为 nullable
,如下所示:
import 'dart:convert';
ChapterBySubjectModel chapterBySubjectModelFromJson(String str) => ChapterBySubjectModel.fromJson(json.decode(str));
String chapterBySubjectModelToJson(ChapterBySubjectModel data) => json.encode(data.toJson());
class ChapterBySubjectModel
ChapterBySubjectModel(
this.success,
this.chapter,
);
int success;
List<Chapter> chapter;
factory ChapterBySubjectModel.fromJson(Map<String, dynamic> json) => ChapterBySubjectModel(
success: json["success"] == null ? null : json["success"],
chapter: json["chapter"] == null ? null : List<Chapter>.from(json["chapter"].map((x) => Chapter.fromJson(x))),
);
Map<String, dynamic> toJson() =>
"success": success == null ? null : success,
"chapter": chapter == null ? null : List<Chapter>.from(chapter.map((x) => x)),
;
【讨论】:
所以我也可以在章节类中设为 null 吗? 是的,你也可以。 章节:json["chapter"] == null ? null : List以上是关于NoSuchMethodError (NoSuchMethodError: 方法 'map' 在 null 上被调用的主要内容,如果未能解决你的问题,请参考以下文章
NoSuchMethodError (NoSuchMethodError: 方法 'map' 在 null 上被调用
NoSuchMethodError(NoSuchMethodError:方法'[]'在null上被调用。接收者:null尝试调用:[](“title”))