gpt4 book ai didi

flutter - Flutter:在null上调用了getter(JSON API调用)

转载 作者:行者123 更新时间:2023-12-03 04:48:31 26 4
gpt4 key购买 nike

我有一个问题,我知道哪里出了问题,但我不知道如何解决。
我是Flutter开发的新手,正在尝试创建一个应用程序。
对于此应用程序,我需要调用返回数据的API,但是根据我的要求,我可以得到不同的返回值。

这是两个例子:

{
"name": "Luken",
"race": "Blood Elf",
"class": "Death Knight",
"active_spec_name": "Blood",
"active_spec_role": "TANK",
"gender": "male",
"faction": "horde",
"achievement_points": 5145,
"honorable_kills": 0,
"thumbnail_url": "https://render-eu.worldofwarcraft.com/character/ysondre/200/100426440-avatar.jpg?alt=wow/static/images/2d/avatar/10-0.jpg",
"region": "eu",
"realm": "Ysondre",
"profile_url": "https://raider.io/characters/eu/ysondre/Luken",
"profile_banner": "hordebanner1",
"guild": null
}

第二个API返回:
{
"name": "Rakhalar",
"race": "Zandalari Troll",
"class": "Druid",
"active_spec_name": "Guardian",
"active_spec_role": "TANK",
"gender": "male",
"faction": "horde",
"achievement_points": 5525,
"honorable_kills": 0,
"thumbnail_url": "https://render-eu.worldofwarcraft.com/character/illidan/213/142815957-avatar.jpg?alt=wow/static/images/2d/avatar/31-0.jpg",
"region": "eu",
"realm": "Illidan",
"profile_url": "https://raider.io/characters/eu/illidan/Rakhalar",
"profile_banner": "hordebanner1",
"guild": {
"name": "Gueule de Bois",
"realm": "Illidan"
}

两次返回之间的主要区别是公会键: 可以为为空。
我不知道如何处理它,因为当我尝试调用字符时的名称或 Realm 值时,它会导致错误(这基本上是返回API的WoW字符的信息)。

我用于序列化JSON的类如下:
class Guild {
String name;
String realm;

Guild({
this.name,
this.realm,
});

factory Guild.fromJson(Map<String, dynamic> json) => Guild(
name: json["name"] == null ? null : json["name"],
realm: json["realm"] == null ? null : json["realm"],
);

Map<String, dynamic> toJson() => {
"name": name == null ? null : name,
"realm": realm == null ? null : realm,
};
}

还有UI部分(以防在 View 方面要做,但我真的不这么认为。)
class CharacterDetailPage extends StatelessWidget {
final CharacterDetailArguments args;

CharacterDetailPage({this.args});

@override
Widget build(BuildContext ctxt) {
return new Scaffold(
appBar: AppBar(
title: Text("Detail page for ${args.name}"),
),
body: FutureBuilder<CharacterDetailApi>(
future: getCharacterDetails(args.name, args.realm, args.region),
builder: (context, snapshot) {
return snapshot.hasData
? Container(
child:
Container(
height: 125,
child: Column(
children: <Widget>[
Text(snapshot.data.name, ),
Text("<" + snapshot.data.guild.name + ">",),
Text(snapshot.data.guild.realm,),
],
),
),
)
}),
);
}
}

我确定这是一个基本问题,但我尚未找到任何相关信息:(

编辑:使用匿名功能,可以使用以下代码:
Column(
children: <Widget>[
Text(snapshot.data.name,),
if (snapshot.data.guild != null)
Text("<" + snapshot.data.guild.name +">",),
if (snapshot.data.guild != null)
Text(snapshot.data.guild.realm,),
],
),

最佳答案

更改此代码

Text("<" + snapshot.data.guild.name + ">",),
Text(snapshot.data.guild.realm,),

对此:
if (snapshot.data.guild != null) {
Text("<" + snapshot.data.guild.name + ">"),
Text(snapshot.data.guild.realm),
}

关于flutter - Flutter:在null上调用了getter(JSON API调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61826509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com