Search code examples
flutternulldart-null-safety

Non-nullable instance field '_selectedSize' must be initialized


I have been working with my Store App, but this null safety is getting me pissed now. I have created a class but it gives me this error with later doesn't allow my app to work correctly
this is the the product.dart file:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:loja_virtual_nnananene/models/item_size.dart';

class Product extends ChangeNotifier {
  Product.fromDocument(DocumentSnapshot document) {
    id = document.documentID;
    name = document['name'];
    description = document['description'];
    images = List<String>.from(document.data['images'] as List<dynamic>);
    // ingore_for_file: Warning: Operand of null-aware operation
    sizes = (document.data['sizes'] as List<dynamic> ?? [])
        .map((s) => ItemSize.fromMap(s as Map<String, dynamic>))
        .toList();
  }

  String id = "";
  String name = "";
  String description = "";
  List<String> images = [];
  List<ItemSize> sizes = [];

  ItemSize _selectedSize;
  ItemSize get selectedSize => _selectedSize;
  set selectedSize(ItemSize value) {
    _selectedSize = value;
    notifyListeners();
  }
}

I'm receiving an error at the Product.from... This is the error:

Non-nullable instance field '_selectedSize' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.

This is my ItemSize class:

class ItemSize {
  ItemSize.fromMap(Map<String, dynamic> map) {
    name = map['name'] as String;
    price = map['price'] as num;
    stock = map['stock'] as int;
  }

  String name = "";
  num price = 0;
  int stock = 0;

  bool get hasStock => stock > 0;

  @override
  String toString() {
    return 'ItemSize{name: $name, price: $price, stock: $stock}';
  }
}

Calling in main widget:

class SizeWidget extends StatelessWidget {
  const SizeWidget(this.size);

  final ItemSize size;

  @override
  Widget build(BuildContext context) {
    final product = context.watch<Product>();
    final selected = size == product.selectedSize;

    Color color;
    if (!size.hasStock)
      color = Colors.red.withAlpha(50);
    else if (selected)
      color = ColorSelect.cprice;

Solution

  • the code tries to get the selected item the first time, and of course it will be null. The alternative I found was...

    in ItemSize class, i create a construtor simple with all null -> ItemSize();

     class ItemSize {
      ItemSize.fromMap(Map<String, dynamic> map) {
        name = map['name'] as String;
        price = map['price'] as num;
        stock = map['stock'] as int;
      }
     
      ItemSize();
     
      String? name;
      num? price;
      int? stock;
     
      bool get hasStock => stock! > 0;
     
      @override
      String toString() {
        return 'ItemSize{name: $name, price: $price, stock: $stock}';
      }
    }
    

    in the Product class do the get this way.

     ItemSize get selectedSize {
        if (_selectedSize != null)
          return _selectedSize!;
        else
          return ItemSize();
      }