dio flutter中解析数据到模型类时Api调用错误

问题描述 投票:0回答:2

错误:

E / flutter(28049):[错误:flutter/runtime/dart_vm_initializer.cc(41)]未处理的异常:'package:object_mapper/src/mappable.dart':断言失败:第8行第12行:'构造函数!= null ': RegistrationResponse 未在 Reflection.factories 中定义 E/flutter (28049): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61) E/flutter (28049): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5) E/flutter (28049): #2 新的 Mappable (package:object_mapper/src/mappable.dart:8:12) E/flutter (28049): #3 Mapper.toObject (包:object_mapper/src/mapper.dart:22:20) E/flutter (28049): #4 DioClient.userRegistration (包:chavara/src/features/registration/service/registration_service.dart:20:45) E/颤振(28049): E/颤振(28049): I/ViewRootImpl@219a9d3MainActivity: ViewPostIme 指针 0 I/ViewRootImpl@219a9d3MainActivity:ViewPostIme 指针 1

代码:

    RegistrationResponse? user;

    try {
      Response userData =await WebUtil.createDio().post('/User',
      data:requestData);
      if (kDebugMode) {
        print('User Info: ${userData.data}');
      }
      user = Mapper.fromJson(userData.data).toObject<RegistrationResponse>();
    } on DioError catch (e) { }
flutter dart parsing dio
2个回答
1
投票
Mappable.factories = {
      RegistrationResponse: () => RegistrationResponse(),
      Relation: () => Relation()
    };

After adding this code in class, the code is working fine...

0
投票

列出用户= userData.map((userData.data) => Mapper.fromJson(data)).toList();

© www.soinside.com 2019 - 2024. All rights reserved.