【问题标题】:NoSuchMethodError: The getter 'avatar' was called on nullNoSuchMethodError:在 null 上调用了 getter 'avatar'
【发布时间】:2020-09-01 13:03:14
【问题描述】:

看过类似的问题,看不到任何常见的错误。一旦工厂似乎可以毫无问题地创建对象。但是,调用任何方法都会生成 NoSuchMethodError。调试了几天,没有想法。使用该一般布局的数据模型有类似的代码,没有问题。

这是数据模型的代码

class Performer {
  String avatar, header, name, username;
  int id, subscribePrice;
  bool isRealPerformer,
      isPerformer,
      hasStories,
      hasStream,
      isPaywallRestriction;

  Performer(
      {this.avatar,
      this.header,
      this.name,
      this.username,
      this.id,
      this.subscribePrice,
      this.isRealPerformer,
      this.isPerformer,
      this.hasStories,
      this.hasStream,
      this.isPaywallRestriction});

  factory Performer.fromJson(Map<String, dynamic> performer) {
    return Performer(
        avatar: performer["avatar"],
        header: performer["header"],
        name: performer["name"],
        username: performer["username"],
        id: performer["id"],
        subscribePrice: performer["subscribePrice"],
        isRealPerformer: performer["isRealPerformer"],
        isPerformer: performer["isPerformer"],
        hasStories: performer["hasStories"],
        hasStream: performer["hasStream"],
        isPaywallRestriction: performer["isPaywallRestriction"]);
  }
}

这是填充模型的代码

Future<List<Performer>> getSubscriptions() async {
    List<Performer> performers = [];

    String url = "some API url";

    String res = await _callServer(url);

    if (res.isNotEmpty) {
      List<dynamic> payload = json.decode(res);

      payload.forEach((element) {
        performers.add(new Performer.fromJson(element));
      });
      return performers;

    } else return performers;
  }

Future<Performer> getPerformer(int performerID) async {
    List<Performer> subs = await getSubscriptions();

    Performer performer;

    int prefIndex;

    for (int x = 0; x < subs.length; x++) {
      if (subs[x].id == performerID){
        performer = subs[x];
        break;
      }
    }

    if (performer.avatar != null) {
      print("found ${performer.username}");
      return performer;
    } else return null;
  }

这是根据模型生成 UI 元素的代码

class ProfilePic extends StatefulWidget {
  final int id;
  ProfilePic({Key key, @required this.id}) : super();

  @override
  State<StatefulWidget> createState() => _profilePicState();
}

class _profilePicState extends State<ProfilePic> {
  Performer performer;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    Backend().getPerformer(widget.id).then((value) {
      performer = value;
      setState(() {});
    });

  }

  @override
  Widget build(BuildContext context) {
    print("profile for: ${widget.id}");
    return Container(
      child: performer == null ? Container() : CircleAvatar(
        radius: 30.0,
        backgroundImage:
        NetworkImage(performer.avatar),
        backgroundColor: Colors.transparent,
      ),
    );
  }

【问题讨论】:

  • 检查API响应,可能是提交的“头像”有拼写错误,因为模型中的头像似乎存储了null。我认为这就是您收到此错误的原因。
  • @theCaptainXgod 不是头像字段为空,而是对象。
  • @Locked 创建了一个调用该函数的按钮,并且所有方法都足够有趣。
  • 但是这个错误仍然存​​在?
  • @theCaptainXgod 创建了一个可以正常工作的测试按钮,但不是 UI 图像代码

标签: android json flutter dart nosuchmethoderror


【解决方案1】:

在你的getSubscriptions,试试

payload.forEach((element) {
        performers.add(new Performer.fromJson(Map.from(element)));
});

【讨论】:

  • 好的。你能在 foreach 循环之前打印res 吗?
  • 在按钮中调用该函数会出于某种原因打印头像
  • @Edmund 之前你在哪里调用函数?
  • 想通了,传递的是帖子 ID 而不是用户 ID,因此出现空错误
【解决方案2】:

代码没有错误,将帖子 ID 而不是用户 ID 返回给函数,因此出现空错误。

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 2018-12-03
    • 2020-06-15
    • 2020-12-26
    • 1970-01-01
    • 2020-08-11
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多