Back

[SOLVED] Null is not a subtype

  • 0
  • Flutter
Ichigor
29 Mar, 2023, 22:09

I have 10 attributes in my collection and when I run the application it gives an error at this point:

TypeScript
    databases ?? init();
    try {
      String userId = await AuthHelper.instance.getUserId() ?? "";
      DocumentList response = await databases!.listDocuments(
        databaseId: "64046c8b50eba8c807d0",
        collectionId: "6405162fd68c7c61df67",
      );
      return response.documents
          .map(
            (e) => TodoModel.fromJson(e.data, e.$id,)
          )
          .toList();
    } catch (e) {
      rethrow;
    }
  }```

Exception has occurred.
_TypeError (type 'Null' is not a subtype of type 'String')

This is what my data model looks like:

class TodoModel { String name; int scores; double progress; List<dynamic> errors; List<dynamic> friends; int alchiki; String refcode; DateTime createdAt; String id; String userId;

TodoModel({ required this.name, required this.scores, required this.progress, required this.errors, required this.friends, required this.alchiki, required this.refcode, required this.createdAt, required this.userId, required this.id, });

TodoModel.fromJson(Map<String, dynamic> json, String tempId) : name = json['first_name'], scores = json['scores'], progress = json['progress'], errors = json['errors'], friends = json['friends'], alchiki = json['alchiki'], refcode = json['refcode'], createdAt = DateTime.parse(json['createdAt']), userId = json['userId'], id = tempId;

Map<String, dynamic> toJson() => { 'first_name': name, 'scores': scores, 'progress': progress, 'errors': errors, 'friends': friends, 'alchiki': alchiki, 'refcode': refcode, 'createdAt': createdAt.toIso8601String(), 'userId': userId, }; } ```

TL;DR
It seems like there's an issue with the `refcode` attribute being null in your data model. One solution is to remove the `refcode` attribute from the model, which should make the application work fine. If you want to keep the `refcode` attribute, make sure to handle null values properly in your code. You can modify the `fromJson` method in your data model as follows: ``` TodoModel.fromJson(Map<String, dynamic> json, String tempId) : name = json['first_name'] ?? "", scores = json['scores'], progress = json['progress'], errors = json['
Ichigor
29 Mar, 2023, 22:09

If I remove the "refcode" attribute from the model, then the application will work fine.

Drake
29 Mar, 2023, 22:30

it seems you have a document where refcode is null, right?

Ichigor
30 Mar, 2023, 14:13

SOLVED

Drake
30 Mar, 2023, 14:18

[SOLVED] Null is not a subtype

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more