Reputation: 765
Im having the problem above, I understand that in my data some things are null for example the leistungData from Diesel doesn't contain anything, so the title is null:
"title":"Benzin",
"leistungData":[
{
"title":"240 PS"
}
]
},
{
"title":"Diesel",
"leistungData":[
]
},
So I get that I get the 'Null' is not a subtype of type 'String'. But how can I change my code below so that I can keep this Json structure?
import 'leistungData.dart';
part 'treibstoffData.g.dart';
@JsonSerializable(explicitToJson: true)
class TreibstoffData {
final String title;
final List <LeistungData> leistungData;
TreibstoffData(this.title, this.leistungData);
factory TreibstoffData.fromJson(Map<String, dynamic> json)
=> _$TreibstoffDataFromJson(json);
Map<String, dynamic> toJson() => _$TreibstoffDataToJson(this);
}
& here the LeistungData:
import 'package:json_annotation/json_annotation.dart';
import 'unterscheidungsData.dart';
part 'leistungData.g.dart';
@JsonSerializable()
class LeistungData {
final String title;
LeistungData(this.title);
factory LeistungData.fromJson(Map<String, dynamic> json)
=> _$LeistungDataFromJson(json);
Map<String, dynamic> toJson() => _$LeistungDataToJson(this);
}
I hope I was clear and didn't forget anything, just write to me if Im missing something
Upvotes: 0
Views: 551
Reputation: 63559
convert to nullable list.
replace final List <LeistungData> leistungData;
with final List <LeistungData>? leistungData;
also try final String? title ;
let me know if it fails to solve the error.
Upvotes: 1