为什么Flutter冻结了来自Json的包,产生了带有意外runtimeType的对象?

20jt8wwn  于 2023-10-21  发布在  Flutter
关注(0)|答案(1)|浏览(139)

我正在调查Flutter冷冻包与以下模型:

import 'package:freezed_annotation/freezed_annotation.dart';
part 'ledger_summary_model.freezed.dart';
part 'ledger_summary_model.g.dart';

@freezed
class LedgerSummary with _$LedgerSummary {
  const factory LedgerSummary({
    required int ledgerId,
  }) = _LedgerSummary;
  factory LedgerSummary.fromJson(Map<String, dynamic> json) =>
      _$LedgerSummaryFromJson(json);
}

测试代码

LedgerSummary ledgerSummary = const LedgerSummary(ledgerId: 1);
    LedgerSummary ledgerSummaryAgain =
        LedgerSummary.fromJson(jsonDecode(jsonEncode(ledgerSummary)));
    print(ledgerSummaryAgain.runtimeType.toString());

Prints _$_LedgerSummary,而我更希望是LedgerSummary。
我的pubspec.yaml是

name: test_freeze
description: A new Flutter project.
publish_to: 'none' 
version: 1.0.0+1
environment:
  sdk: '>=3.1.0 <4.0.0'
dependencies:
  flutter:
    sdk: flutter
  freezed: ^2.4.0
  freezed_annotation: ^2.1.0
  flutter_localizations:
    sdk: flutter
  dart_mappable:
  dart_mappable_builder:
dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^2.1.0
  flutter_launcher_icons: ^0.13.1
  flutter_lints: ^2.0.1
flutter:
  uses-material-design: true

和Flutter医生-v输出是

[√] Flutter (Channel stable, 3.13.1, on Microsoft Windows [Version 10.0.19044.3086], locale et-EE)
    • Flutter version 3.13.1 on channel stable at c:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e1e47221e8 (6 weeks ago), 2023-08-22 21:43:18 -0700
    • Engine revision b20183e040
    • Dart version 3.1.0
    • DevTools version 2.25.0

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\pavel\AppData\Local\Android\Sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = C:\Users\pavel\AppData\Local\Android\Sdk
    • ANDROID_SDK_ROOT = C:\Users\pavel\AppData\Local\Android\Sdk
    • Java binary at: c:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.1.4)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.1.32407.343
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2022.3)
    • Android Studio at c:\Program Files\Android\Android Studio
    • 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
    • android-studio-dir = c:\Program Files\Android\Android Studio
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)

[√] VS Code (version 1.82.2)
    • VS Code at C:\Users\pavel\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.72.0

[√] Connected device (4 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 14 (API 34) (emulator)
    • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.19044.3086]
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 116.0.5845.141
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 117.0.2045.43

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

• No issues found!

我的预期用例是实现一个包含多个冻结类的存储库,这些冻结类将作为通用对象提交给单个滚动视图控制器。

cgyqldqp

cgyqldqp1#

https://github.com/rrousselGit(非常迅速的答复的荣誉)通知,这种行为是设计到包。

相关问题