ITADN

includeIfNull formatter errors

#1527OpenKestasVenslauskas 创建于 2025-09-17
Dart SDK ``` environment: sdk: '>=3.8.0 <4.0.0' ``` Package version ``` json_serializable: ^6.11.1 json_annotation: ^4.9.0 ``` Flutter version ``` Flutter 3.35.3 • channel stable • https://github.com/flutter/flutter.git Tools • Dart 3.9.2 • DevTools 2.48.0 ``` Given this test class which uses `includeIfNull:false`. ``` @JsonSerializable(includeIfNull: false) class FooClass { final String? bar; FooClass({this.bar}); } ``` Generator throwns exception: ``` E An error `UnexpectedOutputException` occurred while formatting the generated source for `package:uh_payments/domain/entities/order_item_body/foo_class.dart` which was output to `lib/domain/entities/order_item_body/foo_class.json_serializable.g.part`. This may indicate an issue in the generator, the input source code, or in the source formatter. The formatter produced unexpected output. Input was: // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** FooClass _$FooClassFromJson(Map<String, dynamic> json)=> FooClass( bar: json['bar'] as String?, ); Map<String, dynamic> _$FooClassToJson(FooClass instance) => <String, dynamic>{ 'bar': ?instance.bar, }; Which formatted to: // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** FooClass _$FooClassFromJson(Map<String, dynamic> json) => FooClass(bar: json['bar'] as String?); Map<String, dynamic> _$FooClassToJson(FooClass instance) => <String, dynamic>{ 'bar': instance.bar, }; ``` Not sure where is the problem. I see that formatted version removes null-aware elements approach. Removing `includeIfNull` helps. But I do need that.
5 条评论