fluttercandies / FlutterJsonBeanFactory

What I do is generate dart beans based on json, as well as generics parameters and json build instances
Apache License 2.0
564 stars 56 forks source link

[Bug report] return convertFuncMap[type]!(value as Map<String, dynamic>) as T; #161

Closed yfming93 closed 4 months ago

yfming93 commented 7 months ago

Version

5.1.5

Platforms

dart

Device Model

redmi note9

flutter info

[!] Flutter (Channel [user-branch], 3.19.0, on macOS 13.5 22G74 darwin-x64, locale zh-Hans-CN)
    ! Flutter version 3.19.0 on channel [user-branch] at /Users/mingo/flutter
      Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
      If that doesn't fix the issue, reinstall Flutter by following instructions at
      https://flutter.dev/docs/get-started/install.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bae5e49bc2 (3 weeks ago), 2024-02-13 17:46:18 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly
      to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/mingo/Library/Android/sdk
    • Platform android-34, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.82.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (4 available)
    • M2103K19C (mobile) • jb95ha7h9hbigm9t          • android-arm64  • Android 13 (API 33)
    • iPhone12T (mobile) • 00008101-000304382222001E • ios            • iOS 17.3.1 21D61
    • macOS (desktop)    • macos                     • darwin-x64     • macOS 13.5 22G74 darwin-x64
    • Chrome (web)       • chrome                    • web-javascript • Google Chrome 122.0.6261.94
    ! Error: Browsing on the local area network for 袁晴iPhone. Ensure the device is unlocked and attached with a cable or
      associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for 罗文俊的iPhone. Ensure the device is unlocked and attached with a cable
      or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

How to reproduce?

asT type '_Map<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>' in type cast #0 JsonConvert._asT

Logs

I/flutter (20199): asT<OssObj> type '_Map<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>' in type cast #0      JsonConvert._asT (package:app/generated/json/base/json_convert_content.dart:160:44)
I/flutter (20199): #1      JsonConvert.convert (package:app/generated/json/base/json_convert_content.dart:85:14)
I/flutter (20199): #2      $SdRoadFenceListFromJson.<anonymous closure> (package:app/generated/json/sd_road.g.dart:654:30)
I/flutter (20199): #3      MappedListIterable.elementAt (dart:_internal/iterable.dart:425:31)
I/flutter (20199): #4      ListIterator.moveNext (dart:_internal/iterable.dart:354:26)
I/flutter (20199): #5      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
I/flutter (20199): #6      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
I/flutter (20199): #7      new List.of (dart:core-patch/array_patch.dart:39:18)
I/flutter (20199): #8      ListIterable.toList (dart:_internal/iterable.dart:224:7)
I/flutter (20199): #9      $SdRoadFenceListFromJson (package:app/generated/json/sd_road.g.dart:654:60)
I/flutter (20199): #10     new SdRoadFenceList.fromJson (package:app/models/v5/sd_road.dart:221:66)
I/flutter (20199): #11     _MapTabPageState.onBMFMapCreated.<anonymous closure> (package:app/pages/home/map_tab_page.dart:239:32)
I/flutter (20199): #12     BMFMethodChannelHandler.handlerMethod (package:flutter_baidu_mapapi_map/src/private/bmf_method_channel_handler.dart:462:44)
I/flutter (20199): #13     BMFMapController._handlerMethod (package:flutter_baidu_mapapi_map/src/map/bmf_map_controller.dart:56:40)
I/flutter (20199): #14     MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:571:55)
I/flutter (20199): #15     MethodChannel.setMethodCallHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:564:34)
I/flutter (20199): #16     _DefaultBinaryMessenger.setMessageHandler.<anonymous closure> (package:flutter/src/services/binding.dart:603:35)
I/flutter (20199): #17     _invoke2 (dart:ui/hooks.dart:344:13)
I/flutter (20199): #18     _ChannelCallbackRecord.invoke (dart:ui/channel_buffers.dart:45:5)
I/flutter (20199): #19     _Channel.push (dart:ui/channel_buffers.dart:135:31)
I/flutter (20199): #20     ChannelBuffers.push (dart:ui/channel_buffers.dart:343:17)
I/flutter (20199): #21     PlatformDispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:737:22)
I/flutter (20199): #22     _dispatchPlatformMessage (dart:ui/hooks.dart:257:31)

Example code (optional)

asT<OssObj> type '_Map<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>' in type cast #0      JsonConvert._asT

Contact

No response

zhangruiyu commented 7 months ago

模型类截图下

yfming93 commented 7 months ago

模型类截图下

image

yfming93 commented 7 months ago

image

yfming93 commented 7 months ago

之前使用5.0.9版本是正常的,从5.0.9直接升级到5.1.5不正常。

zhangruiyu commented 7 months ago

方便给下json和模型类都发我吗 可以加我qq 然后私聊给我

yfming93 commented 7 months ago

方便给下json和模型类都发我吗 可以加我qq 然后私聊给我 sorry。我先自己看看。我这边是显示正常再次交互的不正常。我怀疑是我自己这边的原因。我先确认下

zhangruiyu commented 7 months ago

好的 如果发现问题了 或者 解决 麻烦给反馈下 方便下次其他朋友们遇到这个问题快速解决

yfming93 commented 7 months ago

A//最新版 return convertFuncMap[type]!(value as Map<String, dynamic>) as T; B//老版本 return convertFuncMap[type]!(Map<String, dynamic>.from(value)) as T; 两个版本第一次使用fromJson展示都正常。A// 版本把实例toJson()后得到的JSON再去fromJson就报错如上。B// 版本这些情况都不报错。

toJson()后得到的JSON再去fromJson报错。 测试的JSON:

{
        "id": 57539,
        "path": [
          {
            "uid": "files/20230907/1694069454950300",
            "url": "https://xxx.oss-cn-beijing.aliyuncs.com/files/20230907/1694069454950300.jpeg",
            "name": "files/20230907/1694069454950300.jpeg",
            "size": 129886,
            "type": "jpeg",
            "thumbUrl": "https://xxx-beijing.aliyuncs.com/files/20230907/1694069454950300.jpeg"
          }
        ],
        "reference": [],
        "weather": {
          "rh": 50,
          "temp": 34,
          "text": "晴",
          "uptime": "20230907143000",
          "wind_dir": "东北风",
          "feels_like": 36,
          "wind_class": "2级"
        },
        "status": "assigned",
        "publish_status": 20,
        "publish_time": "2023-09-07 14:51:30",
        "expiry_time": "2023-09-11 23:59:59",
        "expiry_status": 40,
        "expiry_score": "0.00",
        "complaint_expiry_time": "2023-09-11 23:59:59",
        "complete_time": null,
        "rectify_times": 0,
        "remark": null,
        "created_at": "2023-09-07 14:51:30",
        "updated_at": "2023-09-12 00:00:01",
        "patrol_user_name": "xxx",
        "top_score_sort_title": "xxx",
        "score_sort_title": "xxx",
        "evaluate_title": "xxx",
        "patrol_depart_name": "ABC",
        "belong_depart_name": "xx",
        "rectify_depart_name": "xxx",
        "sort_name": "xxx",
        "road_id": 724,
        "tags": []
      }
zhangruiyu commented 7 months ago

image 没发现问题啊

zhangruiyu commented 7 months ago

image 是这么吗

yfming93 commented 7 months ago

正常转换确实无问题,干净无其他业务的demo没有这个问题。我出现这个问题是因为我把toJson后得到的Map<String,dynamic>)数据赋值给百度地图了。然后在业务场景取出使用此时百度地图给我的是Map类型的Json(称为jsn,被强转为Map额不是Map<String,dynamic>))。然后进行转换SdRoadFenceList.fromJson(jsn);此时jsn中的path数组List里面的数据全部是是_Map<Object?, Object?>类型不是Map<String,dynamic>)类型导致转换失败。因为最新转换都是使用return convertFuncMap[type]!(value as Map<String, dynamic>) as T;如果当 value是_Map<Object?, Object?>类型时候 value as Map<String, dynamic>就失败报错了。

不知道新版是因为什么原因把原来的 return convertFuncMap[type]!(Map<String, dynamic>.from(value)) as T;换成在目前这这个方式转换。原来的Map<String, dynamic>.from(value)就适配我说的这个场景不报错。

为了修复这个问题。我对Map做了扩展,把Map类型都转换为Map<String,dynamic>)再去使用你的工具就正常了。 extension MapStrDyn on Map {

Map<String,dynamic> get toStrDynamic { return map((key, value) { if(value.runtimeType.toString().startsWith('List')) return MapEntry(key.toString(), mapLs(value as List)); return MapEntry(key.toString(), value); }); }

List<Map<String,dynamic>> mapLs (List ls) => ls.map((e) => (e as Map).toStrDynamic).toList(); }

最后。如果没有特殊的原因,期望把return convertFuncMap[type]!(value as Map<String, dynamic>) as T; 还是改为原来的 return convertFuncMap[type]!(Map<String, dynamic>.from(value)) as T;

zhangruiyu commented 4 months ago

新版本修复了这个问题 更新下就行 最近工作时间是太忙 没有去修复

zhangruiyu commented 4 months ago

解决方案就是2种都兼容了,之前反复横跳的原因是场景下需要另外一种,导致反复改这块,这次我做泛型判断