मैं अभी सीखता हूं कि मैं स्पंदन में BuiltValue का उपयोग कैसे कर सकता हूं। मेरी कार्यान्वित webservice Chopper के साथ इस पुस्तकालय का उपयोग करने से पहले ठीक काम करती है।

अब उपयोग करने के लिए बदलने के बाद BuiltValue मुझे त्रुटि मिलती है और मैं इस समस्या का समाधान नहीं कर सकता

त्रुटि है:

_InternalLinkedHashMap' 'BuiltRegister' प्रकार का उपप्रकार नहीं है

मेरे सर्वर को केवल mobile_number मिलता है और यह जेसन प्रतिक्रिया संरचना है:

{
  "message": "",
  "userInformation": {
    "active": 1,
    "name": "",
    "email": "aaaa@gmail.com",
    "avatar": "",
    "mobile_number": "5656565656",
    "teammate": 0,
    "experience": null,
    "age": 44,
    "gender": 0,
    "height": 555,
    "weight": 80400,
    "insurance": null,
    "job": "dddddd",
    "created_at": "2019-06-23 09:26:41",
    "updated_at": "2019-08-26 17:51:24",
    "api_token": "5QbgyBdcL"
  },
  "tickets": [
    {
      "id": 3,
      "subject": "3",
      "description": "3",
      "ticket_number": 5273,
      "priority": 1,
      "section": 1,
      "solved": 0,
      "created_at": "2019-06-23 09:26:41",
      "updated_at": "2019-08-26 17:51:24",
      "user": {
        "active": 1,
        "name": "",
        "email": "aaaa@gmail.com",
        "avatar": "",
        "mobile_number": "5656565656",
        "teammate": 0,
        "experience": null,
        "age": 44,
        "gender": 0,
        "height": 555,
        "weight": 80400,
        "insurance": null,
        "job": "dddddd",
        "created_at": "2019-06-23 09:26:41",
        "updated_at": "2019-08-26 17:51:24",
        "api_token": "5QbgyBdcL"
      },
      "reply": [
        {
          "ticket_id": 3,
          "reply": "5050505050505050050505005",
          "created_at": "2019-06-23 09:26:41",
          "updated_at": "2019-08-26 17:51:24",
          "user": {
            "active": 1,
            "name": "",
            "email": "aaaa@gmail.com",
            "avatar": "",
            "mobile_number": "5656565656",
            "teammate": 0,
            "experience": null,
            "age": 44,
            "gender": 0,
            "height": 555,
            "weight": 80400,
            "insurance": null,
            "job": "dddddd",
            "created_at": "2019-06-23 09:26:41",
            "updated_at": "2019-08-26 17:51:24",
            "api_token": "5QbgyBdcL"
          }
        }
      ]
    }
  ],
  "status": 5
}

मैं संरचना के साथ BuiltRegister रजिस्टर बनाता हूं:

part 'built_register.g.dart';

abstract class BuiltRegister implements Built<BuiltRegister, BuiltRegisterBuilder> {
  //@formatter:off
  String              get mobile_number;
  @nullable String    get message;
  @nullable BuiltUser get userInformation;
  @nullable int       get status;
  @nullable BuiltList<BuiltTickets> get tickets;
  //@formatter:on

  BuiltRegister._();

  factory BuiltRegister([updates(BuiltRegisterBuilder b)]) = _$BuiltRegister;

  static Serializer<BuiltRegister> get serializer => _$builtRegisterSerializer;
}

abstract class BuiltUser implements Built<BuiltUser, BuiltUserBuilder> {
  //@formatter:off
  String           get mobile_number;
  String           get active;
  @nullable String get name;
  @nullable String get email;
  @nullable String get avatar;
  @nullable String get teammate;
  @nullable String get experience;
  @nullable String get age;
  @nullable String get gender;
  @nullable String get height;
  @nullable String get weight;
  @nullable String get insurance;
  @nullable String get job;
  String           get created_at;
  String           get updated_at;
  String           get api_token;
  //@formatter:on

  BuiltUser._();

  factory BuiltUser([updates(BuiltUserBuilder b)]) = _$BuiltUser;

  static Serializer<BuiltUser> get serializer => _$builtUserSerializer;
}

abstract class BuiltTickets implements Built<BuiltTickets, BuiltTicketsBuilder> {
  //@formatter:off
  int        get id;
  String     get subject;
  String     get description;
  int        get ticket_number;
  int        get priority;
  int        get section;
  int        get solved;
  String     get created_at;
  String     get updated_at;
  BuiltUser  get user;
  @nullable BuiltReply get reply;
  //@formatter:on

  BuiltTickets._();

  factory BuiltTickets([updates(BuiltTicketsBuilder b)]) = _$BuiltTickets;

  static Serializer<BuiltTickets> get serializer => _$builtTicketsSerializer;
}

abstract class BuiltReply implements Built<BuiltReply, BuiltReplyBuilder> {
  //@formatter:off
  int       get ticket_id;
  String    get reply;
  String    get created_at;
  String    get updated_at;
  BuiltUser get user;
  //@formatter:on

  BuiltReply._();

  factory BuiltReply([updates(BuiltReplyBuilder b)]) = _$BuiltReply;

  static Serializer<BuiltReply> get serializer => _$builtReplySerializer;
}

build कमांड टर्मिनल रिटर्न में सफल रहा और मुझे कोई समस्या नहीं है, अब यह मेरा Chopper कार्यान्वयन है:

part 'api_service.chopper.dart';

@ChopperApi(baseUrl: '/api/v1')
abstract class DiabetistApiService extends ChopperService {
  @Post(path: '/register')
  Future<Response<BuiltRegister>> registerUser(@Body() BuiltRegister body);

  static DiabetistApiService create() {
    final client = ChopperClient(
        baseUrl: 'http://www.example.com',
        services: [
          _$DiabetistApiService(),
        ],
        converter: BuiltValueConvert(),
        interceptors: [
          HeadersInterceptor({'Content-Type': 'application/json'}),
          HttpLoggingInterceptor(),
          (Response response) async {
            if (response.statusCode == 404) {
              chopperLogger.severe('404 NOT FOUND');
            }
          }
        ]);
    return _$DiabetistApiService(client);
  }
}

और BuiltValueConvert Chopper के लिए इंटरसेप्टर कार्यान्वयन:

class BuiltValueConvert extends JsonConverter {
  @override
  Request convertRequest(Request request) {
    //@formatter:off
    return super
        .convertRequest(
                request.replace(
                    body: serializers.serializeWith(
                        serializers.serializerForType(
                            request.body.runtimeType), request.body
                    )
                )
    );
    //@formatter:on
  }

  @override
  Response<BodyType> convertResponse<BodyType, singleItemType>(Response response,) {
    final Response dynamicResponse = super.convertResponse(response);
    final BodyType customBody = _convertToCustomObject(dynamicResponse.body);

    return dynamicResponse.replace<BodyType>(body: customBody);
  }

  dynamic _convertToCustomObject<SingleItemType>(dynamic element) {
    if (element is SingleItemType) return element;

    if (element is List)
      return _deserializerListOf<SingleItemType>(element);
    else
      return _deserialize<SingleItemType>(element);
  }

  BuiltList<SingleItemType> _deserializerListOf<SingleItemType>(List dynamicList) {
    return BuiltList<SingleItemType>(
        dynamicList.map((element) => _deserialize<SingleItemType>(element))
    );
  }

  SingleItemType _deserialize<SingleItemType>(Map<String, dynamic> value) {
    return serializers.deserializeWith(serializers.serializerForType(SingleItemType), value);
  }
}

और इस प्रकार मैं सर्वर से कनेक्ट करना, परिणाम भेजना और प्राप्त करना चाहता हूं:

final register = BuiltRegister((b) => b..mobile_number = mobileNumber);
final response = await Provider.of<DiabetistApiService>(context).registerUser(register);

जब मैं प्रतिक्रिया मुद्रित करने का प्रयास करता हूं तो मुझे त्रुटि मिलती है

1
DolDurma 26 अगस्त 2019, 17:24

1 उत्तर

सबसे बढ़िया उत्तर

समस्या हल हो गई!!!

मैं बदल गया

convertResponse<BodyType, singleItemType> 

प्रति

convertResponse<BodyType, SingleItemType>
1
CopsOnRoad 27 अगस्त 2019, 10:54