Home > Enterprise >  User model sends null data to the api
User model sends null data to the api

Time:06-02

I have an api where i get my users data. I made a user model which holds the data of the user after decoding it. After making some changes and POST to the user resource in the api (e.g profile name) the toJson() method of the user model sends other user object as null values to the api, excluding the updated field.

Expected behavior: The toJson() method supposed to check for null objects and replace it with the previous data gotten from the api instead of sending null to the api.

Is there any way to make the user model holds the user value when sending to the api?

This is my UserModel class

String userModelToJson(UserModel data) => jsonEncode(data);

@JsonSerializable()
class UserModel {
  UserModel({
    this.type,
    this.bio,
    this.name,
    this.interests,
    this.presentation,
    this.links,
    this.location,
    this.school,
    this.occupation,
    this.createdAt,
    this.lastUpdatedAt,
  });

  final String? type;
  final String? bio;
  final String? name;
  final List<dynamic>? interests;
  final Presentation? presentation;
  final Links? links;
  final Location? location;
  final School? school;
  final Occupation? occupation;
  final int? createdAt;
  final int? lastUpdatedAt;

  factory UserModel.fromJson(Map<String, dynamic> json) =>
      _$UserModelFromJson(json);

  Map<String, dynamic> toJson() => _$UserModelToJson(this);
}

json_serializable of user model

UserModel _$UserModelFromJson(Map<String, dynamic> json) => UserModel(
      type: json['type'] as String?,
      bio: json['bio'] as String?,
      name: json['name'] as String?,
      interests: json['interests'] as List<dynamic>?,
      presentation: json['presentation'] == null
          ? null
          : Presentation.fromJson(json['presentation'] as Map<String, dynamic>),
      links: json['links'] == null
          ? null
          : Links.fromJson(json['links'] as Map<String, dynamic>),
      location: json['location'] == null
          ? null
          : Location.fromJson(json['location'] as Map<String, dynamic>),
      school: json['school'] == null
          ? null
          : School.fromJson(json['school'] as Map<String, dynamic>),
      occupation: json['occupation'] == null
          ? null
          : Occupation.fromJson(json['occupation'] as Map<String, dynamic>),
      createdAt: json['createdAt'] as int?,
      lastUpdatedAt: json['lastUpdatedAt'] as int?,
    );

Map<String, dynamic> _$UserModelToJson(UserModel instance) => <String, dynamic>{
      'type': instance.type,
      'bio': instance.bio,
      'name': instance.name,
      'interests': instance.interests,
      'presentation': instance.presentation,
      'links': instance.links,
      'location': instance.location,
      'school': instance.school,
      'occupation': instance.occupation,
    };

Get user method 

  Future<UserModel> getUserData() async {
    UserModel? user;
    final token = await _storage.getKey("Token");
    ApiService.client.options.headers['Authorization'] = 'Bearer $token';
    final response = await ApiService.client.get(ApiEndpoint.profile);
    if (response.statusCode == 200 || response.statusCode == 201) {
      user = UserModel.fromJson(response.data);
      return user;
    } else {
      throw "Couldn't load user";
    }
  }

CodePudding user response:

create file static_variable.dart

class StaticVariable {
  static UserModel? user;
}

save user data to StaticVariable

Future<UserModel> getUserData() async {
    UserModel? user;
    final token = await _storage.getKey("Token");
    ApiService.client.options.headers['Authorization'] = 'Bearer $token';
    final response = await ApiService.client.get(ApiEndpoint.profile);
    if (response.statusCode == 200 || response.statusCode == 201) {
      user = UserModel.fromJson(response.data);
      StaticVariable.user = user; //save user variable to StaticVariable
      return user;
    } else {
      throw "Couldn't load user";
    }
  }

use ?? operation for check null

Map<String, dynamic> _$UserModelToJson(UserModel instance) => <String, dynamic>{
      'type': instance.type ?? StaticVariable!.user.type,
      'bio': instance.bio ?? StaticVariable!.user.bio,
      'name': instance.name ?? StaticVariable!.user.name,
      'interests': instance.interests ?? StaticVariable!.user.interests,
      'presentation': instance.presentation ?? StaticVariable!.user.presentation,
      'links': instance.links ?? StaticVariable!.user.links,
      'location': instance.location ?? StaticVariable!.user.location,
      'school': instance.school ?? StaticVariable!.user.school,
      'occupation': instance.occupation ?? StaticVariable!.user.ccupation,
    };

'type': instance.type ?? "", mean:

if(instance.type == null){
  'type': StaticVariable.user.type
} else {
  'type': instance.type
}
  • Related