-1
ChangeFavoritesModel? changeFavoritesModel;
  void changeFavorites(int productId) {
    favorites?[productId] = !favorites[productId];
    DioHelper.postData(
      url: FAVORITES,
      data: {
        'product_id': productId,
      },
      token: token,
    ).then((value) {
      changeFavoritesModel = ChangeFavoritesModel.fromJson(value.data);
      print(value.data);
      emit(ShopSuccessChangeFavoritesState());
    }).catchError((error) {
      emit(ShopErrorChangeFavoritesState());
    });
  }

here,an error occurs:

favorites?[productId] = !favorites[productId];

and say:-

A nullable expression can't be used as a condition. (Documentation)
Try checking that the value isn't 'null' before using it as a condition.

1 Answers1

0

because favorites is nullable, so check null first

if (favorites != null) {
    favorites[productId] = !favorites[productId];
}

or if you could do an early return

if (favorites == null) {
    return;
}
adali
  • 5,977
  • 2
  • 33
  • 40