Home > Software engineering >  How not to print a null value received from sqflite database?
How not to print a null value received from sqflite database?

Time:09-27

I'm new to Flutter and I'm making a "Recipes app" to learn. I have this problem now:
I'm working with Sqflite and I have a page where I show the ingredients from the recipe, the sql query is OK, but the problem is that I'm getting the null values and I don't know what to do to hide them. I've tried conditions, operators like (condition ? X : Y), substitutions, and nothing seems to work. PS: Some variables are in spanish 'cause I talk that language.

IngredientModel:

class IngredienteModel {
  IngredienteModel(
      {this.idIngrediente,
      this.idListaIngredientes,
      this.amount,
      this.unit,
      this.name});

  int? idIngrediente;
  int? idListaIngredientes;
  String? amount;
  String? unit;
  String? name;

  factory IngredienteModel.fromJson(Map<String, dynamic> json) =>
      IngredienteModel(
          idIngrediente: json["id_ingrediente"],
          idListaIngredientes: json["id_lista_ingredientes"],
          amount: json["amount"],
          unit: json["unit"],
          name: json["name"]);

  Map<String, dynamic> toJson() => {
        "id_ingrediente": idIngrediente,
        "id_lista_ingredientes": idListaIngredientes,
        "amount": amount,
        "unit": unit,
        "name": name,
      };
}

The widget where I return the ingredients to show:

List<Widget> ingredientesList(
    List<IngredienteModel>? ingredientes, BuildContext context) {
  final List<Widget> _listaIng = [];
  int _index = 1;
  ingredientes!.forEach((element) {
    final _widgetTemp = Row(
          children: [
            Text(_index.toString()   ": "),
            Text(element.amount.toString()   " "),
            _unitText(element),
            Text(element.name.toString()),
          ],
        );
    _listaIng.add(_widgetTemp);
    _index = _index   1;
  });

  return _listaIng;
}
 
Widget _unitText(IngredienteModel ingmod) {
  if (ingmod.unit?.isEmpty) {
    return Text(" ");
  } else {
    return Text(ingmod.unit   " de ");
  }
}
 

In that last method _unitText I've tried many stuff to do the magic, but:

-I can't use "ingmod.unit?.isEmpty" in the conditional because:

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

-Can't use either "ingmod.unit.length == 0" because: The property 'length' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').

-Then, to return the "Ingredient unit Text" asumming that isn't null, I can't use " " to add Strings: "The operator ' ' can't be unconditionally invoked because the receiver can be 'null'. Try adding a null check to the target ('!').

If I fix the errors to run the app, I get this (as you can see, null values are visible):
[1]: https://i.stack.imgur.com/vDI6F.png

Thanks if you reached so far, hope you can help me. Greetings and happy coding!

CodePudding user response:

Try in all the fields of your fromJson method to put

json [variable] ?? DEFAULT VALUE

This what it does is that if that variable is not in the json, it sets that value by default

  • Related