如何不打印从 sqflite 数据库收到的空值?

How not to print a null value received from sqflite database?

我正在制作一个“食谱应用程序”来学习。我现在遇到这个问题:
我正在使用 Sqflite,我有一个页面显示食谱中的成分,sql 查询没问题,但问题是我得到的是空值,我不知道该怎么做隐藏它们。我已经尝试过条件、操作符(条件?X:Y)、替换,但似乎没有任何效果。 PS:有些变量是西班牙语,因为我会说那种语言。

成分模型:

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,
      };
}

我 return 显示成分的小部件:

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 ");
  }
}
 

在最后一种方法中 _unitText 我已经尝试了很多方法来实现魔法,但是:

-我不能在条件语句中使用“ingmod.unit?.isEmpty”,因为:

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

-不能使用“ingmod.unit.length == 0”,因为: 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 ('!').

-然后,对于 return 假设不为空的“成分单位文本”,我不能使用“+”添加字符串: "The operator '+' can't be unconditionally invoked because the receiver can be 'null'. Try adding a null check to the target ('!').

如果我修复 运行 应用程序的错误,我会得到这个(如您所见,空值可见):

尝试在你的fromJson方法的所有字段中放入

json [variable] ?? DEFAULT VALUE

它的作用是,如果该变量不在 json 中,它会默认设置该值