带有 bloc 模式的 TextFormField 正在提交 null
TextFormField with bloc pattern is Subbmiting null
当使用 TextFormField 时,我无法添加或更新数据,提交到数据库的数据始终为空,或者我得到一个 NoSuchMethodErrorerror
I/flutter ( 6511): Another exception was thrown: NoSuchMethodError: The getter 'value' was called on null
我有 bui Bloc 是这样的:
class ProductsBloc {
String id;
// Product _product;
// ignore: close_sinks
// static final _productController = BehaviorSubject<Product>();
// Stream<Product> get productOut => _productController.stream;
// ignore: close_sinks
final _id = BehaviorSubject<int>();
// ignore: close_sinks
final _title = BehaviorSubject<String>();
// ignore: close_sinks
final _message = BehaviorSubject<String>();
// ignore: close_sinks
final _price = BehaviorSubject<String>();
Observable<int> get idOut => _id.stream;
Observable<String> get titleOut => _title.stream.transform(_validateTitle);
Observable<String> get message => _message.stream;
Observable<String> get price => _price.stream;
Function(int) get changeId => _id.sink.add;
Function(String) get changeTitle => _title.sink.add;
Function(String) get changeMessage => _message.sink.add;
Function(String) get changePrice => _price.sink.add;
final _validateTitle = StreamTransformer<String, String>.fromHandlers(handleData: (title, sink){
if(title.isNotEmpty){
sink.add(title);
} else {
sink.addError('Add some text');
}
});
Future<void> createProduct({title}) {
return db.createProduct(DateTime.now().millisecondsSinceEpoch.toString(), title.value, _message.value, _price.value);
}
ui 像这样:
class _ProductEditPageState extends State<ProductEditPage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final _titleFocusNode = FocusNode();
final _descriptionFocusNode = FocusNode();
final _priceFocusNode = FocusNode();
final id;
final title;
final message;
final price;
_ProductEditPageState(this.id, this.title, this.message, this.price);
final Map<String, dynamic> _formData = {
'title': null,
'message': null,
'price': null,
'image': 'assets/food.jpg'
};
Widget _buildTitleTextField(ProductsBloc productBloc) {
return StreamBuilder(
stream: productBloc.titleOut,
builder: (context, snapshot) {
return TextFormField(
focusNode: _titleFocusNode,
onSaved: (String value){ _formData['title'] = value;},
initialValue: title,
decoration: InputDecoration(labelText: 'Title', errorText: snapshot.error),
);
},
);
}
然后像这样提交。
提交的数据因为如果我将提交更改为无参数或它用 null 填充数据库的参数,则会捕获错误 NoSuchMethodFound。
child: Form(
key: _formKey,
child: ListView(
padding: EdgeInsets.symmetric(horizontal: targetPadding / 2),
children: <Widget>[
_buildTitleTextField(productBloc, ),
_buildDescriptionTextField(productBloc),
_buildPriceTextField(productBloc),
SizedBox(
height: 10.0,
),
RaisedButton(
child: Text('Save'),
textColor: Colors.white,
onPressed: () {
if(id != null) {
productBloc.updateData(id);
}
else{
productBloc.createProduct(
title: _formData['title'],
);
}
Navigator.of(context).pop();
},
这也是我的模型
class Product {
final db = Firestore.instance.collection('products');
Future<void> createProduct(String docId, String title, String message, String price) async {
await db.document(docId).setData({'id': docId, 'title': title, 'message': message, 'price': price});
}
void readData(String docId){
db.document(docId).get();
}
Future<void> deleteData(String docId) async {
await db.document(docId).delete();
}
Future<void> updateData(String docId, String title, String message, String price) async {
await db.document(docId).updateData({'title': title, 'message': message, 'price': price});
}
}
Product db = Product();
我也在使用 flutter 的 provider 包,所以我认为 provider 是对的:
return MultiProvider(
providers: [
Provider<ThemeBloc>(
value: ThemeBloc(),
),
Provider<UserBloc>(
value: UserBloc(),
),
Provider<ProductsBloc>(
value: ProductsBloc(),
),
],
child: StreamBuilder<ThemeData>(
使用 textFields 工作正常,但我需要在编辑前查看填充了一些初始数据的表单,因此显然我需要 TextFormFields。
您的代码非常混乱,但据我所知,您在 onSaved
中写入 _formData['title']
,但您从未调用 FormState.save()
。
您试过在保存按钮的 onPressed
中调用 _formKey.currentState.save()
吗?
当使用 TextFormField 时,我无法添加或更新数据,提交到数据库的数据始终为空,或者我得到一个 NoSuchMethodErrorerror
I/flutter ( 6511): Another exception was thrown: NoSuchMethodError: The getter 'value' was called on null
我有 bui Bloc 是这样的:
class ProductsBloc {
String id;
// Product _product;
// ignore: close_sinks
// static final _productController = BehaviorSubject<Product>();
// Stream<Product> get productOut => _productController.stream;
// ignore: close_sinks
final _id = BehaviorSubject<int>();
// ignore: close_sinks
final _title = BehaviorSubject<String>();
// ignore: close_sinks
final _message = BehaviorSubject<String>();
// ignore: close_sinks
final _price = BehaviorSubject<String>();
Observable<int> get idOut => _id.stream;
Observable<String> get titleOut => _title.stream.transform(_validateTitle);
Observable<String> get message => _message.stream;
Observable<String> get price => _price.stream;
Function(int) get changeId => _id.sink.add;
Function(String) get changeTitle => _title.sink.add;
Function(String) get changeMessage => _message.sink.add;
Function(String) get changePrice => _price.sink.add;
final _validateTitle = StreamTransformer<String, String>.fromHandlers(handleData: (title, sink){
if(title.isNotEmpty){
sink.add(title);
} else {
sink.addError('Add some text');
}
});
Future<void> createProduct({title}) {
return db.createProduct(DateTime.now().millisecondsSinceEpoch.toString(), title.value, _message.value, _price.value);
}
ui 像这样:
class _ProductEditPageState extends State<ProductEditPage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final _titleFocusNode = FocusNode();
final _descriptionFocusNode = FocusNode();
final _priceFocusNode = FocusNode();
final id;
final title;
final message;
final price;
_ProductEditPageState(this.id, this.title, this.message, this.price);
final Map<String, dynamic> _formData = {
'title': null,
'message': null,
'price': null,
'image': 'assets/food.jpg'
};
Widget _buildTitleTextField(ProductsBloc productBloc) {
return StreamBuilder(
stream: productBloc.titleOut,
builder: (context, snapshot) {
return TextFormField(
focusNode: _titleFocusNode,
onSaved: (String value){ _formData['title'] = value;},
initialValue: title,
decoration: InputDecoration(labelText: 'Title', errorText: snapshot.error),
);
},
);
}
然后像这样提交。 提交的数据因为如果我将提交更改为无参数或它用 null 填充数据库的参数,则会捕获错误 NoSuchMethodFound。
child: Form(
key: _formKey,
child: ListView(
padding: EdgeInsets.symmetric(horizontal: targetPadding / 2),
children: <Widget>[
_buildTitleTextField(productBloc, ),
_buildDescriptionTextField(productBloc),
_buildPriceTextField(productBloc),
SizedBox(
height: 10.0,
),
RaisedButton(
child: Text('Save'),
textColor: Colors.white,
onPressed: () {
if(id != null) {
productBloc.updateData(id);
}
else{
productBloc.createProduct(
title: _formData['title'],
);
}
Navigator.of(context).pop();
},
这也是我的模型
class Product {
final db = Firestore.instance.collection('products');
Future<void> createProduct(String docId, String title, String message, String price) async {
await db.document(docId).setData({'id': docId, 'title': title, 'message': message, 'price': price});
}
void readData(String docId){
db.document(docId).get();
}
Future<void> deleteData(String docId) async {
await db.document(docId).delete();
}
Future<void> updateData(String docId, String title, String message, String price) async {
await db.document(docId).updateData({'title': title, 'message': message, 'price': price});
}
}
Product db = Product();
我也在使用 flutter 的 provider 包,所以我认为 provider 是对的:
return MultiProvider(
providers: [
Provider<ThemeBloc>(
value: ThemeBloc(),
),
Provider<UserBloc>(
value: UserBloc(),
),
Provider<ProductsBloc>(
value: ProductsBloc(),
),
],
child: StreamBuilder<ThemeData>(
使用 textFields 工作正常,但我需要在编辑前查看填充了一些初始数据的表单,因此显然我需要 TextFormFields。
您的代码非常混乱,但据我所知,您在 onSaved
中写入 _formData['title']
,但您从未调用 FormState.save()
。
您试过在保存按钮的 onPressed
中调用 _formKey.currentState.save()
吗?