android - NoSuchMethodError(NoSuchMethodError:在 null 上调用了 getter 'length'。接收方:null 尝试调用:length)
问题描述
类 AddProduct.dart
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'],
));
}
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 ?? 0 > 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;
});
if (_getCategories() == true) {
return categories;
} else {
return categories.length;
}
}
changeSelectedCategory(String selectedCategory) {
setState(() => _currentCategory = selectedCategory);
}
}
Categories.dar 类
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:uuid/uuid.dart';
class CategoryService {
// ignore: deprecated_member_use
FirebaseFirestore _firestore = FirebaseFirestore.instance;
String ref = 'categories';
void createCategory(String name) {
var id = Uuid();
String categoryId = id.v1();
_firestore.collection(ref).document(categoryId).setData({'category': name});
}
Future<List<DocumentSnapshot>> getCategories() =>
_firestore.collection(ref).getDocuments().then((snaps) {
snaps.documents;
});
Future<List<DocumentSnapshot>> getSuggestions(String suggestion) => _firestore
.collection(ref)
.where('category', isEqualTo: suggestion)
.getDocuments()
.then((snap) {
print(snap.documents.length);
return snap.documents;
});
}
错误是:发生异常。NoSuchMethodError (NoSuchMethodError: The getter 'length' was called on null. Receiver: null 尝试调用: length) 请帮帮我?我不知道我该怎么办,我一直在尝试修复它这几天。我需要你在票价基础上添加这个,但我不能为这个错误¿任何想法?
解决方案
推荐阅读
- json - 如何扩展 JSON 字符串中的 bash 变量?
- performance - 有没有办法在 nagios/icinga 性能数据中为标签添加多个值
- python - 对 neo4j 节点进行时间版本控制的最佳方法是什么?
- database - 通过 API 创建 MongoDB Atlas 数据库的副本
- reactjs - 仅在带有道具的路线上做出反应而不进行 api 调用
- sql - 为什么我违反了完整性约束?(ORA-02291)
- flutter - Flutter Bloc:为什么在通知父 BlocListener 之前使用新状态构建子 BlocBuilder?
- amazon-web-services - AWS EMR Bootstrap action "aws s3 cp ..." to download 11GB file failing due to [Errno 28] No space left on device
- html - 当我在 css 中使用 float:left 时,固定在顶部的导航栏菜单中的文本会移动一点
- javascript - 显示从 url 到 objectUrl 的 base64 图像