I am filtering a list as follows:
return Container(
child: FutureBuilder(
future: fetchCremas(),
builder: (context, snapshot) {
if (snapshot.hasData) {
var filteredList = snapshot.data;
filteredList = filteredList
.where((element) => (element.nombre
.toLowerCase()
.contains(
_controller.text.toLowerCase())
|| element.descripcion.toLowerCase().contains(
_controller.text.toLowerCase())
|| element.sub_cat_nombre.toLowerCase().contains(
_controller.text.toLowerCase())
|| element.sub_cat2_nombre.toLowerCase().contains(
_controller.text.toLowerCase())
|| element.sub_cat3_nombre.toLowerCase().contains(
_controller.text.toLowerCase())
))
.toList();
The issue is that element.sub_cat3_nombre may be empty or null.
Using this code I am getting an error
The method 'toLowerCase' was called on null.
Receiver: null
Tried calling: toLowerCase()
How can I keep filtering on element.sub_cat3_nombre taking into account that it may be empty or null?
CodePudding user response:
some value is Nullable String not String
handled it from backend return String without Nullable , to avoid extra cast or check-null
CodePudding user response:
This should work:
element.sub_cat3_nombre?.toLowerCase()
CodePudding user response:
Could you try this?
filteredList.where((element) => (element.nombre?.toLowerCase() ?? here put replacement object
or you could use if(element.nombre != null)
before you start filtering