简体   繁体   English

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

[英]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'.我正在创建modeljsonjson to dart 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:这是我的 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));只需更改_balance?.add(Dynamic.fromJson(v)); to _balance?.add(v);_balance?.add(v); or create a Balance class yourself, then you can use it like _balance?.add(Balance.fromJson(v));或者自己创建一个Balance class,然后你可以像_balance?.add(Balance.fromJson(v)); , The plugin is unable to completely transform into models its reading dynamic as a class. , 该插件无法将其阅读dynamic完全转换为模型 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写解析此 JSON 的 Dart 模型类? - How do I write a Dart model class which parses this JSON? Flutter:如何将Json转换为dart中的model class - Flutter: How to convert Json to model class in dart 我正在尝试将 Json 数据转换为 model class 在 ZC047B10EEE71CC268 但我收到“Flutter FormatException:意外字符”错误 - I'm trying to prase the Json data to model class in Flutter. but i'm getting "Flutter FormatException: Unexpected character" Error 如何将 json 数组转换为 model class in flutter dart/ - How to convert json array to model class in flutter dart/ Flutter/Dart 解析 JSON 到 model 具有 Map 属性 - Flutter/Dart Parsing JSON to model with a Map attribute 将 Dart/Flutter 中的 Json 解析为实体/模型 - Parse Json in Dart/Flutter to Entity/Model 什么是flutter/Dart中的factory和formJson Model class? - What is factory and formJson in flutter/Dart Model class? 如何 model class 以序列化 Dart Z497031794414A553B435F90151ZAC<string, list> ) 进入 Flutter 中的 JSON?</string,> - How to model class in order to serialize Dart Object (Map<String, List>) into JSON in Flutter? Flutter/Dart JSON 和现有库类的序列化 - Flutter/Dart JSON and serialization of an existing library class 从 model class 获取 null 值,该值与 Z466DEEC76ECDF5FCA6D38571F63file24D54 绑定 - Getting null value from model class which is bind with a json file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM