Home > Software design >  Should we use "late" for Model to fix safety-null error
Should we use "late" for Model to fix safety-null error

Time:10-17

class ItemModel {

late int _page;
  late int _total_results;
  late int _total_pages;
  late List<_Result> _results = [];

  ItemModel.fromJson(Map<String, dynamic> parsedJson) {
    // debugPrint(parsedJson['results'].length);
    _page = parsedJson['page'];
    _total_results = parsedJson['total_results'];
    _total_pages = parsedJson['total_pages'];
    List<_Result> temp = [];
    for (int i = 0; i < parsedJson['results'].length; i  ) {
      _Result result = _Result(parsedJson['results'][i]);
      temp.add(result);
    }
    _results = temp;
  }

  List<_Result> get results => _results;

  int get total_pages => _total_pages;

  int get total_results => _total_results;

  int get page => _page;
}

Can I use late to fix Non-nullable instance field '_page' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it "late"? error.

Or I can use ?

int? _page;

For ? : i get this error A value of type 'int?' can't be returned from the function 'page' because it has a return type of 'int'

Which would be better?

Thanks for your reply!

CodePudding user response:

I would personally make a constructor for your object and make your JSON parsing a factory constructor returning an instance of your class. By doing so, we can skip the use of late variables:

class ItemModel {
  final int page;
  final int total_results;
  final int total_pages;
  final List<_Result> results;

  const ItemModel({
    required this.page,
    required this.total_results,
    required this.total_pages,
    required this.results,
  });

  factory ItemModel.fromJson(Map<String, dynamic> parsedJson) => ItemModel(
        page: parsedJson['page'] as int,
        total_results: parsedJson['total_results'] as int,
        total_pages: parsedJson['total_pages'] as int,
        results: [
          for (var i = 0; i < parsedJson['results'].length; i  )
            _Result(parsedJson['results'][i])
        ],
      );
}

Of if you still wants your getters:

class ItemModel {
  final int _page;
  final int _total_results;
  final int _total_pages;
  final List<_Result> _results;

  const ItemModel({
    required int page,
    required int total_results,
    required int total_pages,
    required List<_Result> results,
  })  : _page = page,
        _total_results = total_results,
        _total_pages = total_pages,
        _results = results;

  factory ItemModel.fromJson(Map<String, dynamic> parsedJson) => ItemModel(
        page: parsedJson['page'] as int,
        total_results: parsedJson['total_results'] as int,
        total_pages: parsedJson['total_pages'] as int,
        results: [
          for (var i = 0; i < parsedJson['results'].length; i  )
            _Result(parsedJson['results'][i])
        ],
      );

  List<_Result> get results => _results;
  int get total_pages => _total_pages;
  int get total_results => _total_results;
  int get page => _page;
}
  • Related