简体   繁体   中英

Flutter getting error in model class which I create through json to dart plugin

I'm creating model of json through json to dart plugin and I'm getting an error Undefined name 'Dynamic'.

Getting error on this line of code:

json['balance'].forEach((v) {
    _balance?.add(Dynamic.fromJson(v));
  });

Is error from my side or backend developer side?

Here is my model class:

import 'dart:convert';

SignupResponse signupResponseFromJson(String str) =>
    SignupResponse.fromJson(json.decode(str));

String signupResponseToJson(SignupResponse data) => json.encode(data.toJson());

class SignupResponse {
  SignupResponse({
    bool? success,
    String? enMessage,
    String? arMessage,
    Data? data,
    int? status,
  }) {
    _success = success;
    _enMessage = enMessage;
    _arMessage = arMessage;
    _data = data;
    _status = status;
  }

  SignupResponse.fromJson(dynamic json) {
    _success = json['success'];
    _enMessage = json['en_message'];
    _arMessage = json['ar_message'];
    _data = json['data'] != null ? Data.fromJson(json['data']) : null;
    _status = json['status'];
  }

  bool? _success;
  String? _enMessage;
  String? _arMessage;
  Data? _data;
  int? _status;

  SignupResponse copyWith({
    bool? success,
    String? enMessage,
    String? arMessage,
    Data? data,
    int? status,
  }) =>
      SignupResponse(
        success: success ?? _success,
        enMessage: enMessage ?? _enMessage,
        arMessage: arMessage ?? _arMessage,
        data: data ?? _data,
        status: status ?? _status,
      );

  bool? get success => _success;

  String? get enMessage => _enMessage;

  String? get arMessage => _arMessage;

  Data? get data => _data;

  int? get status => _status;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['success'] = _success;
    map['en_message'] = _enMessage;
    map['ar_message'] = _arMessage;
    if (_data != null) {
      map['data'] = _data?.toJson();
    }
    map['status'] = _status;
    return map;
  }
}

Data dataFromJson(String str) => Data.fromJson(json.decode(str));

String dataToJson(Data data) => json.encode(data.toJson());

class Data {
  Data({
    User? user,
  }) {
    _user = user;
  }

  Data.fromJson(dynamic json) {
    _user = json['user'] != null ? User.fromJson(json['user']) : null;
  }

  User? _user;

  Data copyWith({
    User? user,
  }) =>
      Data(
        user: user ?? _user,
      );

  User? get user => _user;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    if (_user != null) {
      map['user'] = _user?.toJson();
    }
    return map;
  }
}

User userFromJson(String str) => User.fromJson(json.decode(str));

String userToJson(User data) => json.encode(data.toJson());

class User {
  User({
    String? firstName,
    String? username,
    String? email,
    int? type,
    String? address,
    int? roleId,
    int? verificationCode,
    int? verified,
    String? phone,
    String? mobile,
    String? categoryId,
    String? companyName,
    String? tradeLicense,
    String? field,
    String? workTime,
    String? updatedAt,
    String? createdAt,
    int? id,
    List<dynamic>? balance,
  }) {
    _firstName = firstName;
    _username = username;
    _email = email;
    _type = type;
    _address = address;
    _roleId = roleId;
    _verificationCode = verificationCode;
    _verified = verified;
    _phone = phone;
    _mobile = mobile;
    _categoryId = categoryId;
    _companyName = companyName;
    _tradeLicense = tradeLicense;
    _field = field;
    _workTime = workTime;
    _updatedAt = updatedAt;
    _createdAt = createdAt;
    _id = id;
    _balance = balance;
  }

  User.fromJson(dynamic json) {
    _firstName = json['first_name'];
    _username = json['username'];
    _email = json['email'];
    _type = json['type'];
    _address = json['address'];
    _roleId = json['role_id'];
    _verificationCode = json['verification_code'];
    _verified = json['verified'];
    _phone = json['phone'];
    _mobile = json['mobile'];
    _categoryId = json['category_id'];
    _companyName = json['company_name'];
    _tradeLicense = json['trade_license'];
    _field = json['field'];
    _workTime = json['work_time'];
    _updatedAt = json['updated_at'];
    _createdAt = json['created_at'];
    _id = json['id'];
    if (json['balance'] != null) {
      _balance = [];
      json['balance'].forEach((v) {
        _balance?.add(Dynamic.fromJson(v));
      });
    }
  }

  String? _firstName;
  String? _username;
  String? _email;
  int? _type;
  String? _address;
  int? _roleId;
  int? _verificationCode;
  int? _verified;
  String? _phone;
  String? _mobile;
  String? _categoryId;
  String? _companyName;
  String? _tradeLicense;
  String? _field;
  String? _workTime;
  String? _updatedAt;
  String? _createdAt;
  int? _id;
  List<dynamic>? _balance;

  User copyWith({
    String? firstName,
    String? username,
    String? email,
    int? type,
    String? address,
    int? roleId,
    int? verificationCode,
    int? verified,
    String? phone,
    String? mobile,
    String? categoryId,
    String? companyName,
    String? tradeLicense,
    String? field,
    String? workTime,
    String? updatedAt,
    String? createdAt,
    int? id,
    List<dynamic>? balance,
  }) =>
      User(
        firstName: firstName ?? _firstName,
        username: username ?? _username,
        email: email ?? _email,
        type: type ?? _type,
        address: address ?? _address,
        roleId: roleId ?? _roleId,
        verificationCode: verificationCode ?? _verificationCode,
        verified: verified ?? _verified,
        phone: phone ?? _phone,
        mobile: mobile ?? _mobile,
        categoryId: categoryId ?? _categoryId,
        companyName: companyName ?? _companyName,
        tradeLicense: tradeLicense ?? _tradeLicense,
        field: field ?? _field,
        workTime: workTime ?? _workTime,
        updatedAt: updatedAt ?? _updatedAt,
        createdAt: createdAt ?? _createdAt,
        id: id ?? _id,
        balance: balance ?? _balance,
      );

  String? get firstName => _firstName;

  String? get username => _username;

  String? get email => _email;

  int? get type => _type;

  String? get address => _address;

  int? get roleId => _roleId;

  int? get verificationCode => _verificationCode;

  int? get verified => _verified;

  String? get phone => _phone;

  String? get mobile => _mobile;

  String? get categoryId => _categoryId;

  String? get companyName => _companyName;

  String? get tradeLicense => _tradeLicense;

  String? get field => _field;

  String? get workTime => _workTime;

  String? get updatedAt => _updatedAt;

  String? get createdAt => _createdAt;

  int? get id => _id;

  List<dynamic>? get balance => _balance;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['first_name'] = _firstName;
    map['username'] = _username;
    map['email'] = _email;
    map['type'] = _type;
    map['address'] = _address;
    map['role_id'] = _roleId;
    map['verification_code'] = _verificationCode;
    map['verified'] = _verified;
    map['phone'] = _phone;
    map['mobile'] = _mobile;
    map['category_id'] = _categoryId;
    map['company_name'] = _companyName;
    map['trade_license'] = _tradeLicense;
    map['field'] = _field;
    map['work_time'] = _workTime;
    map['updated_at'] = _updatedAt;
    map['created_at'] = _createdAt;
    map['id'] = _id;
    if (_balance != null) {
      map['balance'] = _balance?.map((v) => v.toJson()).toList();
    }
    return map;
  }
}

Simply change _balance?.add(Dynamic.fromJson(v)); to _balance?.add(v); or create a Balance class yourself, then you can use it like _balance?.add(Balance.fromJson(v)); , The plugin is unable to completely transform into models its reading dynamic as a class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM