Flutter:getter 被调用为空(JSON API 调用)

Flutter : The getter was called on null (JSON API call)

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

这里有两个例子:

{
  "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
} 

第二个APIreturn:

{
  "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"
  }

这两个 return 的主要区别在于公会密钥:它 可以 为空。 我不知道如何处理它,因为当我尝试调用 namerealm 值时会导致错误角色(return API 的 WoW 角色的基本信息)。

我用来序列化 JSON 的 Class 如下:

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 部分(以防在视图方面需要做一些事情,但我真的不这么认为。)

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),
}