Filled Stacks
Filled Stacks

Reputation: 4346

Could not generate toJson code for because of type

When attempting to generate my freezed data models with the toJson function i get the following error.

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

This started happening in Flutter 2.0 and up. The type is defined and compiles fine, when I try to generate the code I get that error. I am using:

Flutter 2.0.3

freezed: 0.12.7 freezed_annotation: 0.12.0

json_annotation: 3.1.1 json_serializable: 3.5.1

build_runner: 1.11.1

I have also updated everything to the latest using a null-safe flutter project and it still happens so I know it's not a null safety issue. Or at least I can assume that given it doesn't work when the project is updated to be null-safe.

This is the smallest reproducible set of models

@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;
}

I have an issue for it here. Which hasn't been answered in over 21 days or even just looked at. I don't know if this is in freezed or in json serializable. Here is a --verbose log for the builder output when the error happens. I've removed some duplicate logs to keep it shorter since it's a lot of text.

 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

I want to be able to generate my models code without getting a type error for a type already defined.

Upvotes: 2

Views: 4158

Answers (1)

Filled Stacks
Filled Stacks

Reputation: 4346

I found out what the problem is. For each of the models I had some additional from json logic I wrote it like this.

 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']),
    );
  }

This caused the generator to ignore the json code generation. Which I discovered from this comment. So I updated all my custom fromJson functions to this.

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']),
    );
  }

The important part is to ensure that fromJson function has => otherwise it will be ignored for some reason.

Upvotes: 1

Related Questions