Home > Back-end >  Error null operation after upgrading flutter
Error null operation after upgrading flutter

Time:10-19

I have a model class where Api is getting parsed

    class MovieDetail {
      final int id;
      final bool adult;
      final int budget;
      final List<Genre> genres;
      final List<Company> companies;
      final String releaseDate;
      final int runtime;
...

this is the whole full code of movie response:

import 'movie_detail.dart';

class MovieDetailResponse {
  final MovieDetail movieDetail;
  final String error;

  MovieDetailResponse(this.movieDetail, this.error);

  MovieDetailResponse.fromJson(Map<String, dynamic> json)
      : movieDetail = MovieDetail.fromJson(json),
        error = "";

  MovieDetailResponse.withError(String errorValue)
      : movieDetail = MovieDetail(null, null, null, null, null, "", null),
        error = errorValue;
}

this is the exact part where it show the error:

     MovieDetailResponse.withError(String errorValue)
          : movieDetail = MovieDetail(null, null, null, null, null, "", null),
            error = errorValue;

I'm getting an error after upgrading flutter, here is the error log:

The argument type 'Null' can't be assigned to the parameter type 'int'. The argument type 'Null' can't be assigned to the parameter type 'bool'. The argument type 'Null' can't be assigned to the parameter type 'int'. The argument type 'Null' can't be assigned to the parameter type 'List'. The argument type 'Null' can't be assigned to the parameter type 'List'. The argument type 'Null' can't be assigned to the parameter type 'int'.

so what can i do to define null when it returns null?

CodePudding user response:

If you want to insert null value in your model's property then you have to make those properties nullable like this.

class MovieDetail {
   final int? id;
   final bool? adult;
   final int? budget;
   ...
}

And try to access the value with null safe operator like this:

final movieDetails = MovieDetails(1, null, null, ...);
log(movieDetails?.id)
log(movieDetails?.adult)

CodePudding user response:

You need to create a model which accepts a null value as well so your model should be as below.

class MovieDetail {
  int? _id;
  bool? _adult;
  int? _budget;
  int? _runtime;
  String? _releaseDate;

  MovieDetail({
    int? id,
    bool? adult,
    int? budget,
    int? runtime,
    String? releaseDate,
  }) {
    _id = id;
    _adult = adult;
    _budget = budget;
    _runtime = runtime;
    _releaseDate = releaseDate;
  }

  int? get id => _id;
  bool? get adult => _adult;
  int? get budget => _budget;
  int? get runtime => _runtime;
  String? get releaseDate => _releaseDate;
  
//and so one....
}

You can access the values of int as below.

final movieDetails = MovieDetails(id:1);
debugPrint(movieDetails?.id)
int id = movieDetails?.id ?? 0; // which returns 0 when the id value is null.

If you want that your value for int is not null then you can create functions as well in your model class for getting the int value even when it is null.

class MovieDetail {
/// Same as above movie class with below functions

int getMovieId(){
   return id ?? 0; // if id is available it will return id else 0 value
}

bool isAdult(){
   return adult ?? false; // By default it is false.
} 
/// and so on...

You can also set the default values to the variables as well using the same named constructor.

  • Related