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

时间:2020-05-15 19:00:52

标签: flutter dart jsonapi-serialize

我有一个问题,我知道哪里出了问题,但我不知道如何解决。 我是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"
  }

两次返回之间的主要区别是公会键:可以为空。 我不知道如何处理它,因为当我尝试调用字符时的 name 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部分(以防在视图方面做某事,但我真的不这么认为。)

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

1 个答案:

答案 0 :(得分:1)

更改此代码

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