0

尝试使用 toJson 函数生成冻结的数据模型时,出现以下错误。

Could not generate toJson code for merchants because of type Merchant.

这开始发生在 Flutter 2.0 及更高版本中。类型已定义并且编译良好,当我尝试生成代码时出现该错误。我在用:

颤振 2.0.3

冻结:0.12.7 freezed_annotation:0.12.0

json_annotation:3.1.1 json_serializable:3.5.1

build_runner:1.11.1

我还使用 null-safe 颤振项目将所有内容更新到最新版本,但它仍然会发生,所以我知道这不是 null 安全问题。或者至少我可以假设当项目更新为空安全时它不起作用。

这是最小的可重现模型集

@freezed
abstract class SearchInformation with _$SearchInformation {
  SearchInformation._();

  factory SearchInformation({
    String query,
    // List<CartProduct> products,
    List<Merchant> merchants,
  }) = _SearchInformation;

  bool get hasSearchResults =>
      (products != null && products.isNotEmpty) ||
      (merchants != null && merchants.isNotEmpty);

  int get searchCount => (products?.length ?? 0) + (merchants?.length ?? 0);

  factory SearchInformation.fromJson(Map<String, dynamic> json) =>
      _$SearchInformationFromJson(json);
}

@freezed
abstract class Merchant with _$Merchant {
  factory Merchant({
    int id,
  }) = _Merchant;
}

我在这里有一个问题。超过 21 天没有回答,甚至只是看了看。我不知道这是冻结还是 json 可序列化。这是发生错误时生成器输出的 --verbose 日志。我删除了一些重复的日志以使其更短,因为它有很多文本。

 flutter pub run build_runner build --delete-conflicting-outputs --verbose
[  +68 ms] executing: [C:\src\flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[  +64 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[        ] 60bd88df915880d23877bfc1602e8ddcf4c4dd2a
[        ] executing: [C:\src\flutter/] git tag --points-at 60bd88df915880d23877bfc1602e8ddcf4c4dd2a
[  +57 ms] Exit code 0 from: git tag --points-at 60bd88df915880d23877bfc1602e8ddcf4c4dd2a
[        ] 2.0.0
[  +34 ms] executing: [C:\src\flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[  +35 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/stable
[        ] executing: [C:\src\flutter/] git ls-remote --get-url origin
[  +32 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/flutter/flutter.git
[  +66 ms] executing: [C:\src\flutter/] git rev-parse --abbrev-ref HEAD
[  +34 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[        ] stable
[  +71 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[  +11 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[   +2 ms] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[   +8 ms] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[  +49 ms] Artifact Instance of 'MaterialFonts' is not required, skipping update.
[        ] Artifact Instance of 'GradleWrapper' is not required, skipping update.
[        ] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[   +8 ms] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[        ] Artifact Instance of 'FlutterSdk' is not required, skipping update.
[   +1 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IosUsbArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FontSubsetArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'PubDependencies' is not required, skipping update.
[  +24 ms] Using C:\src\flutter\.pub-cache for the pub cache.
[   +4 ms] executing: C:\src\flutter\bin\cache\dart-sdk\bin\pub.bat run build_runner build --delete-conflicting-outputs --verbose
[INFO] Generating build script...
[INFO] Generating build script completed, took 390ms

[WARNING] Deleted previous snapshot due to missing asset graph.
[INFO] Creating build script snapshot......
[INFO] Creating build script snapshot... completed, took 10.0s

[INFO] BuildDefinition:Initializing inputs
[INFO] BuildDefinition:Building new asset graph...
[INFO] BuildDefinition:Building new asset graph completed, took 1.1s   

[INFO] BuildDefinition:Checking for unexpected pre-existing outputs....
[INFO] BuildDefinition:Deleting 2 declared outputs which already existed on disk.       
[INFO] BuildDefinition:Checking for unexpected pre-existing outputs. completed, took 2ms

[INFO] Build:Running build...
[INFO] Heartbeat:1.1s elapsed, 0/16 actions completed.
[FINE] freezed:freezed on test/graphql_responses/test_available_merchants.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/graphql_responses/test_general_responses.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/graphql_responses/test_merchant_menu.dart:Running FreezedGenerator
[INFO] Heartbeat:4.3s elapsed, 3/19 actions completed.
[INFO] Heartbeat:5.4s elapsed, 3/19 actions completed.
[INFO] Heartbeat:6.6s elapsed, 3/19 actions completed.
[INFO] Heartbeat:7.8s elapsed, 3/19 actions completed.
[INFO] Heartbeat:9.0s elapsed, 3/19 actions completed.
[INFO] Heartbeat:10.0s elapsed, 3/19 actions completed.
[INFO] Heartbeat:11.0s elapsed, 3/19 actions completed.
[INFO] Heartbeat:12.4s elapsed, 3/19 actions completed.
[INFO] Heartbeat:13.8s elapsed, 3/19 actions completed.
[INFO] Heartbeat:15.2s elapsed, 3/19 actions completed.
[INFO] Heartbeat:16.3s elapsed, 3/19 actions completed.
[INFO] Heartbeat:17.6s elapsed, 3/19 actions completed.
[WARNING] Heartbeat:
No actions completed for 15.0s, waiting on:
  - freezed:freezed on test/database_integration_test.dart
  - freezed:freezed on test/data_serialisation_tests/model_properties_test.dart
  - freezed:freezed on test/data_serialisation_tests/model_converters_test.dart
  - freezed:freezed on test/firebase_mock.dart
  - freezed:freezed on test/delivery_dudes_gql_test.dart
  .. and 11 more

[INFO] Heartbeat:18.9s elapsed, 3/19 actions completed.
[INFO] Heartbeat:19.9s elapsed, 3/19 actions completed.
[INFO] Heartbeat:21.0s elapsed, 3/19 actions completed.
[INFO] Heartbeat:22.1s elapsed, 3/19 actions completed.
[INFO] Heartbeat:23.1s elapsed, 3/19 actions completed.
FINE] freezed:freezed on test/data_serialisation_tests/model_properties_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/delivery_dudes_gql_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/algolia_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/delivery_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/firebase_mock.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/remote_config_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/stripe_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/utils/card_helpers_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/utils/discount_helper_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/utils/firebase_exception_helper_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/authentication_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/api_integration_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/database_integration_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/cart_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/order_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/merchant_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/data_serialisation_tests/model_converters_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/search_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/push_notification_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/overlay_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/test_data.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/services_tests/user_service_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/test_helpers.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/utils/graphql_parser_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/utils/image_helper_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/utils/order_status_helper_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/utils/string_helper_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/utils/time_helper_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/address_bottom_sheet_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/add_payment_method_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/address_details_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/add_card_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/address_selection_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/browse_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/change_password_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/checkouterror_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/checkout_waiting_room_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/checkout_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/delivery_bottom_sheet_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/create_profile_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/delivery_addresses_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/enroute_driver_banner_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/driver_tracking_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/favourites_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/forgot_password_confirmation_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/guest_mode_bottom_sheet_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/help_center_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/home_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/merchant_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/login_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/order_details_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/order_history_details_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/order_history_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/api/delivery_dudes_api.dart:Running FreezedGenerator
[FINE] freezed:freezed on test/viewmodel_tests/order_status_viewmodel_test.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/api/delivery_dudes_gql_api.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/api/graph_ql_parser.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/api/graph_ql_queries.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/app/flavor_config.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/app/lifecycle_manager.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/app/locator.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/app/logger.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/constants/app_constants.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/constants/app_strings.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/app/app.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/database/dddatabase.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/database/_fake_data.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/datamodels/application_models.dart:Running FreezedGenerator
[FINE] freezed:freezed on lib/datamodels/data_converters.dart:Running FreezedGenerator
...
NE] json_serializable:json_serializable on test/viewmodel_tests/browse_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/browse_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/change_password_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/change_password_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/checkouterror_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/checkouterror_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/checkout_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/checkout_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/checkout_waiting_room_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/checkout_waiting_room_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/create_profile_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/create_profile_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/delivery_addresses_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/delivery_addresses_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/delivery_bottom_sheet_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/delivery_bottom_sheet_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/driver_tracking_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/driver_tracking_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/enroute_driver_banner_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/enroute_driver_banner_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/favourites_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/favourites_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/forgot_password_confirmation_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/forgot_password_confirmation_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/guest_mode_bottom_sheet_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/guest_mode_bottom_sheet_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/help_center_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/help_center_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/home_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/home_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/login_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/login_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/merchant_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/merchant_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/order_details_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/order_details_viewmodel_test.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on test/viewmodel_tests/order_history_details_viewmodel_test.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on 
[FINE] json_serializable:json_serializable on lib/constants/app_constants.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/constants/app_strings.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/constants/app_strings.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/database/dddatabase.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/database/dddatabase.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/database/_fake_data.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/database/_fake_data.dart:Running JsonLiteralGenerator - 2 of 2
[FINE] json_serializable:json_serializable on lib/datamodels/application_models.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/datamodels/data_converters.dart:Running JsonSerializableGenerator - 1 of 2
[FINE] json_serializable:json_serializable on lib/datamodels/data_converters.dart:Running JsonLiteralGenerator - 2 of 2
[SEVERE] json_serializable:json_serializable on lib/datamodels/application_models.dart:

Could not generate `toJson` code for `merchants` because of type `Merchant`.
package:customer_app/datamodels/application_models.freezed.dart:2174:24
     ╷
2174 │   final List<Merchant> merchants;
     │                        ^^^^^^^^^
     ╵
package:json_serializable/src/encoder_helper.dart 133:7  EncodeHelper._serializeField
package:json_serializable/src/encoder_helper.dart 59:42  EncodeHelper._writeToJsonSimple.<fn>
dart:core                                                StringBuffer.writeAll
package:json_serializable/src/encoder_helper.dart 56:9   EncodeHelper._writeToJsonSimple
package:json_serializable/src/encoder_helper.dart 44:7   EncodeHelper.createToJson.sync_op
...
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/search/search_results.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/search/search_suggestions.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/search/search_view.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/search/search_viewmodel.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/select_previous_order_bottom_sheet/select_previous_order_bottom_sheet.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/shared/base_app_viewmodel.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/signup/signup_view.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/signup/signup_viewmodel.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/startup/startup_view.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/startup/startup_viewmodel.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/status_tracking_viewmodel.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/user_information/user_information_view.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/welcome/welcome_view.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/user_information/user_information_viewmodel.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/ui/views/welcome/welcome_viewmodel.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/util/card_helpers.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/util/discount_helper.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/util/firebase_exception_helper.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/util/image_helper.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/util/map_helper.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/util/order_status_helper.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/util/platform_helper.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/util/snackbar_helper.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/util/time_helper.dart:Running StackedRouterGenerator
[FINE] stacked_generator:stackedRouterGenerator on lib/app/locator.config.dart:Running StackedRouterGenerator
[INFO] Build:Running build completed, took 2m 5s

[INFO] Build:Caching finalized dependency graph...
[INFO] Heartbeat:2m 6s elapsed, 3101/3101 actions completed.
[INFO] Build:Caching finalized dependency graph completed, took 1.2s

[SEVERE] Build:
Failed after 2m 6s
[+140810 ms] "flutter run" took 140 951ms.
[   +5 ms] pub finished with exit code 1
[  +35 ms] 
           #0      throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
           #1      _DefaultPub.interactively (package:flutter_tools/src/dart/pub.dart:364:7)
           <asynchronous suspension>
           #2      PackagesForwardCommand.runCommand (package:flutter_tools/src/commands/packages.dart:238:5)
           <asynchronous suspension>
           #3      FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:1157:12)
           <asynchronous suspension>
           #4      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1009:27)
           <asynchronous suspension>
           #5      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #6      AppContext.run (package:flutter_tools/src/base/context.dart:149:12)
           <asynchronous suspension>
           #7      CommandRunner.runCommand (package:args/command_runner.dart:197:13)
           <asynchronous suspension>
           #8      FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:278:9)
           <asynchronous suspension>
           #9      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #10     AppContext.run (package:flutter_tools/src/base/context.dart:149:12)
           <asynchronous suspension>
           #11     FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:234:5)
           <asynchronous suspension>
           #12     run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:64:9)
           <asynchronous suspension>
           #13     run.<anonymous closure> (package:flutter_tools/runner.dart:62:12)
           <asynchronous suspension>
           #14     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
           <asynchronous suspension>
           #15     AppContext.run (package:flutter_tools/src/base/context.dart:149:12)
           <asynchronous suspension>
           #16     runInContext (package:flutter_tools/src/context_runner.dart:73:10)
           <asynchronous suspension>
           #17     main (package:flutter_tools/executable.dart:90:3)
           <asynchronous suspension>


[ +364 ms] ensureAnalyticsSent: 252ms
[   +2 ms] Running shutdown hooks
[  +14 ms] Shutdown hooks complete
[   +2 ms] exiting with code 1

我希望能够生成我的模型代码,而不会出现已定义类型的类型错误。

4

1 回答 1

1

我发现了问题所在。对于每个模型,我都有一些额外的 json 逻辑,我是这样写的。

 factory Merchant.fromJson(Map<String, dynamic> incomingJson) {
    var jsonPayload = Map<String, dynamic>.from(incomingJson);

    if (jsonPayload['availableTimes'] is String) {
      jsonPayload['availableTimes'] =
          json.decode(jsonPayload['availableTimes']);
    }

    if (jsonPayload['territoryAvailableTimes'] is String) {
      jsonPayload['territoryAvailableTimes'] =
          json.decode(jsonPayload['territoryAvailableTimes']);
    }

    if (jsonPayload['cuisines'] is String) {
      jsonPayload['cuisines'] = json.decode(jsonPayload['cuisines']);
    }

    return _$MerchantFromJson(jsonPayload).copyWith(
      // Convert the bool to an integer and set it to the hideCustomizations value
      hideCustomizations: boolToIntOrInt(jsonPayload['hideCustomizations']),
    );
  }

这导致生成器忽略了 json 代码生成。我从这个评论中发现的。所以我将我所有的自定义 fromJson 函数更新为这个。

factory Merchant.fromJson(Map<String, dynamic> incomingJson) =>
      _customFromJson(incomingJson);

  static Merchant _customFromJson(Map<String, dynamic> incomingJson) {
    var jsonPayload = Map<String, dynamic>.from(incomingJson);

    if (jsonPayload['availableTimes'] is String) {
      jsonPayload['availableTimes'] =
          json.decode(jsonPayload['availableTimes']);
    }

    if (jsonPayload['territoryAvailableTimes'] is String) {
      jsonPayload['territoryAvailableTimes'] =
          json.decode(jsonPayload['territoryAvailableTimes']);
    }

    if (jsonPayload['cuisines'] is String) {
      jsonPayload['cuisines'] = json.decode(jsonPayload['cuisines']);
    }

    if (jsonPayload['subVendors'] is String) {
      jsonPayload['subVendors'] = json.decode(jsonPayload['subVendors']);
    }

    return _$MerchantFromJson(jsonPayload).copyWith(
      // Convert the bool to an integer and set it to the hideCustomizations value
      hideCustomizations: boolToIntOrInt(jsonPayload['hideCustomizations']),
    );
  }

重要的部分是确保 fromJson 函数具有,=>否则它将由于某种原因被忽略。

于 2021-03-29T11:53:19.320 回答