android - 错误颤动NoSuchMethodError(NoSuchMethodError:在null上调用了getter'length'。接收者:null
问题描述
class AddProduct extends StatefulWidget {
@override
_AddProductState createState() => _AddProductState();
}
class _AddProductState extends State<AddProduct> {
CategoryService _categoryService = CategoryService();
BrandService _brandService = BrandService();
GlobalKey<FormState> _formKey = GlobalKey<FormState>();
TextEditingController prodoctNameController = TextEditingController();
List<DocumentSnapshot> brands = <DocumentSnapshot>[];
List<DocumentSnapshot> categories = <DocumentSnapshot>[];
List<DropdownMenuItem<String>> categoriesDropDown =
<DropdownMenuItem<String>>[];
List<DropdownMenuItem<String>> brandsDropDown = <DropdownMenuItem<String>>[];
String _currentCategory = "test";
String _brandCategory;
Color white = Colors.white;
Color black = Colors.black;
Color grey = Colors.grey;
Color blue = Colors.blue;
@override
void initState() {
_getCategories();
//_getBrands();
categoriesDropDown = getCategoriesDropDown();
//_currentCategory = categoriesDropDown[0].value;
}
List<DropdownMenuItem<String>> getCategoriesDropDown() {
List<DropdownMenuItem<String>> items = new List();
for (DocumentSnapshot category in categories) {
items.add(new DropdownMenuItem(
child: Text(category['category']),
value: category['category'].value ?? "",
));
}
return items;
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: white,
leading: Icon(
Icons.close,
color: black,
),
title: Text(
"Añadir producto",
style: TextStyle(color: black),
),
),
body: Form(
key: _formKey,
child: ListView(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: OutlineButton(
borderSide:
BorderSide(color: grey.withOpacity(0.8), width: 1.0),
onPressed: () {},
child: Padding(
padding: const EdgeInsets.fromLTRB(14, 40, 14, 40),
child: new Icon(
Icons.add,
color: grey,
),
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: OutlineButton(
borderSide:
BorderSide(color: grey.withOpacity(0.8), width: 1.0),
onPressed: () {},
child: Padding(
padding: const EdgeInsets.fromLTRB(14, 40, 14, 40),
child: new Icon(
Icons.add,
color: grey,
),
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: OutlineButton(
borderSide:
BorderSide(color: grey.withOpacity(0.8), width: 1.0),
onPressed: () {},
child: Padding(
padding: const EdgeInsets.fromLTRB(14, 40, 14, 40),
child: new Icon(
Icons.add,
color: grey,
),
),
),
),
)
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Indicar nombre del producto',
textAlign: TextAlign.center,
style: TextStyle(
color: blue,
fontSize: 12,
),
),
),
Padding(
padding: const EdgeInsets.all(12.0),
child: TextFormField(
controller: prodoctNameController,
decoration: InputDecoration(hintText: "Añadir nombre producto"),
// ignore: missing_return
validator: (value) {
if (value.isEmpty ?? "") {
return 'Por favor, introduzca el nombre';
} else if (value.length > 10) {
return 'No se puede tener mas de 10 letras';
}
},
),
),
Center(
child: DropdownButton(
value: _currentCategory,
items: categoriesDropDown,
onChanged: changeSelectedCategory),
)
],
),
),
);
}
_getCategories() async {
List<DocumentSnapshot> data = await _categoryService.getCategories();
print(data.length ?? "");
setState(() {
categories = data;
});
}
changeSelectedCategory(String selectedCategory) {
setState(() => _currentCategory = selectedCategory);
}
}
错误是这样的:
发生异常。NoSuchMethodError(NoSuchMethodError:在 null 上调用了 getter 'length'。接收方:null 尝试调用:length)
我需要将此添加到 Firebase。但我不能因为这个错误。
解决方案
推荐阅读
- python - 使用 Python 请求登录网站后无法抓取数据
- java - 从流中写入
到 PrintWriter 掉线 - php - 有没有办法用php关闭窗口?
- json - Swift - 遍历结构数组
- google-cloud-platform - Google Cloud Speech to text 返回空结果或错误
- python - SELECT查询PostgreSQL中的西里尔符号
- django - django 捕获特定名称下的所有 URL
- grafana - Grafana - 在 JSON 模型中修改“全部”标签
- sql - 验证值是否存在的存储过程
- android - 文件夹不是在 Android 6.0.1 中创建的,而是在 Androd 5 和 Android 10 上工作的?