Reputation: 329
can anyone solve the error of null operation in my code?
I'm already changing the operator for many times and im still confusing why this still show the error, this is the highlighted error:
StreamBuilder<CastResponse>(
stream: castsBloc.subject.stream,
builder: (context, AsyncSnapshot<CastResponse> snapshot) {
if (snapshot.hasData) {
if (snapshot.data?.error != null &&
snapshot.data?.error.length > 0) {
return _buildErrorWidget(snapshot.data.error);
}
return _buildCastWidget(snapshot.data);
} else if (snapshot.hasError) {
return _buildErrorWidget(snapshot.error);
} else {
return _buildLoadingWidget();
}
},
)
],
);
}
and also this one:
Widget _buildCastWidget(CastResponse data) {
List<Cast> casts = data.casts;
if you wondering how the model looks like:
class Cast {
final int id;
final String character;
final String name;
final String img;
Cast(this.id,
this.character,
this.name,
this.img);
Cast.fromJson(Map<String, dynamic> json)
: id = json["cast_id"],
character = json["character"],
name = json["name"],
img = json["profile_path"];
}
can you help me to solve it if you know how
Upvotes: 1
Views: 77
Reputation: 402
Flutter shows an error because snapshot.data
can be null.
You check with the previous if
statements that it is not null, so you can safely change snapshot.data?.error
to snapshot.data!.error
The !
operator simply tells flutter that the nullable variable is not null, so you will stop seeing the error.
Upvotes: 1