在 Dio Flutter 中将数据解析为模型类时发生 API 调用错误。

huangapple go评论70阅读模式
英文:

Api calling error while parsing the data to model class in dio flutter

问题

错误:

E/flutter (28049): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常: 'package:object_mapper/src/mappable.dart': 断言失败: 第8行第12位: 'constructor != null': 在Reflection.factories中未定义RegistrationResponse
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 new Mappable (package:object_mapper/src/mappable.dart:8:12)
E/flutter (28049): #3 Mapper.toObject (package:object_mapper/src/mapper.dart:22:20)
E/flutter (28049): #4 DioClient.userRegistration (package:chavara/src/features/registration/service/registration_service.dart:20:45)
E/flutter (28049): <异步暂停>
E/flutter (28049):
I/ViewRootImpl@219a9d3MainActivity: ViewPostIme 指针 0
I/ViewRootImpl@219a9d3MainActivity: ViewPostIme 指针 1

代码:

    RegistrationResponse? user;

    try {
      Response userData =await WebUtil.createDio().post(&#39;/User&#39;,
      data:requestData);
      if (kDebugMode) {
        print(&#39;User Info: ${userData.data}&#39;);
      }
      user = Mapper.fromJson(userData.data).toObject&lt;RegistrationResponse&gt;();
    } on DioError catch (e) { }

<details>
<summary>英文:</summary>

Error:

E/flutter (28049): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: &#39;package:object_mapper/src/mappable.dart&#39;: Failed assertion: line 8 pos 12: &#39;constructor != null&#39;: RegistrationResponse is not defined in 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      new Mappable (package:object_mapper/src/mappable.dart:8:12)
E/flutter (28049): #3      Mapper.toObject (package:object_mapper/src/mapper.dart:22:20)
E/flutter (28049): #4      DioClient.userRegistration (package:chavara/src/features/registration/service/registration_service.dart:20:45)
E/flutter (28049): &lt;asynchronous suspension&gt;
E/flutter (28049): 
I/ViewRootImpl@219a9d3[MainActivity](28049): ViewPostIme pointer 0
I/ViewRootImpl@219a9d3[MainActivity](28049): ViewPostIme pointer 1

code:

```` Future&lt;RegistrationResponse?&gt; userRegistration({required RegisterInfoReq requestData}) async {
    RegistrationResponse? user;

    try {
      Response userData =await WebUtil.createDio().post(&#39;/User&#39;,
      data:requestData);
      if (kDebugMode) {
        print(&#39;User Info: ${userData.data}&#39;);
      }
      user = Mapper.fromJson(userData.data).toObject&lt;RegistrationResponse&gt;();
    } on DioError catch (e) { }

</details>


# 答案1
**得分**: 1

```
Mappable.factories = {
      RegistrationResponse: () =&gt; RegistrationResponse(),
      Relation: () =&gt; Relation()
    };

添加了这段代码后,类的代码就正常运行了...
```

<details>
<summary>英文:</summary>

```
Mappable.factories = {
      RegistrationResponse: () =&gt; RegistrationResponse(),
      Relation: () =&gt; Relation()
    };

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

</details>



# 答案2
**得分**: 0

List&lt;RegistrationResponse&gt; users = userData.map((userData.data) =&gt; Mapper.fromJson(data)).toList();

<details>
<summary>英文:</summary>

List&lt;RegistrationResponse&gt; users =
userData.map((userData.data) =&gt; Mapper.fromJson(data)).toList();


</details>



huangapple
  • 本文由 发表于 2023年3月15日 20:26:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定