Search code examples
androidjsonflutterdartnosuchmethoderror

NoSuchMethodError: The getter 'avatar' was called on null


Have looked at similar questions, can't see any common mistakes. Once the factory seems to create the object with no issues. However, calling any of the methods generates a NoSuchMethodError. Been debugging for days, out of ideas. Have similar code using data models of that general layout with no issues.

This is the code for the data model

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"]);
  }
}

This is the code that populates the models

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;
  }

This is the code that generates the UI element based on the model

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

Solution

  • Nothing wrong with the code was returning the post ID not the User ID to the function, hence the null error.