こんにちは皆さん、私のページ「product_edit」にエラーチェックコードを追加して、潜在的なhttp応答エラーを処理しようとしていました
これが私のページです
product_edit.dart
==============================
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import '../models/product.dart';
import '../scoped-models/main.dart';
class ProductEdit extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _ProductEditState();
}
}
class _ProductEditState extends State<ProductEdit> {
final Map<String, dynamic> _formData = {
'title': null,
'description': null,
'price': null,
'image':
'https://cdn.pixabay.com/photo/2020/03/04/07/28/black-4900736_960_720.jpg'
};
//String _title;
// String _description;
// double _price;
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
Widget _buildtypetextfield(Product product) {
return TextFormField(
decoration: InputDecoration(labelText: 'Product Type'),
initialValue: product == null ? '' : product.title,
validator: (String value) {
if (value.isEmpty) {
return 'product type and name is required';
}
return null;
},
onSaved: (String value) {
_formData['title'] = value;
},
);
}
Widget _builddescriptiontextfield(Product product) {
return TextFormField(
maxLines: 6,
decoration: InputDecoration(labelText: 'Description'),
initialValue: product == null ? '' : product.description,
validator: (String value) {
if (value.isEmpty || value.length < 10) {
return 'product description is required,and should be 10+ characters long';
}
return null;
},
onSaved: (String value) {
_formData['description'] = value;
},
);
}
Widget _buildpricetextfield(Product product) {
return TextFormField(
keyboardType: TextInputType.number,
decoration: InputDecoration(labelText: 'Price'),
initialValue: product == null ? '' : product.price.toString(),
validator: (String value) {
if (value.isEmpty ||
!RegExp(r'^(?:[1-9]\d*|0)?(?:\.\d+)?$').hasMatch(value)) {
return 'product price is required,and should be a number';
}
return null;
},
onSaved: (String value) {
_formData['price'] = double.parse(value);
},
);
}
Widget _buildSubmitButton() {
return ScopedModelDescendant<MainModel>(
builder: (BuildContext context, Widget child, MainModel model) {
return model.isloading
? Center(child: CircularProgressIndicator())
: RaisedButton(
child: Text('Save'),
textColor: Colors.white,
onPressed: () => _submitform(
model.addproduct,
model.updateproduct,
model.selectproduct,
model.selectedProductIndex),
);
},
);
}
Widget _buildPageContent(BuildContext context, Product product) {
final double deviceWidth = MediaQuery.of(context).size.width;
final double targetWidth = deviceWidth > 550.0 ? 500.0 : deviceWidth * 0.95;
final double targetPadding = deviceWidth - targetWidth;
return GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(FocusNode());
},
child: Container(
margin: EdgeInsets.all(10.0),
child: Form(
key: _formkey,
child: ListView(
padding: EdgeInsets.symmetric(horizontal: targetPadding / 2),
children: <Widget>[
_buildtypetextfield(product),
_builddescriptiontextfield(product),
_buildpricetextfield(product),
SizedBox(
height: 10.0,
),
_buildSubmitButton(),
],
),
),
),
);
}
void _submitform(
Function addproduct, Function updateproduct, Function setSelectedProduct,
[int selectedproductIndex]) {
if (!_formkey.currentState.validate()) {
return;
}
_formkey.currentState.save();
//final Map<String, dynamic> product = {
// 'title': _title,
// 'description': _description,
// 'price': _price,
// 'image':
// 'https://cdn.pixabay.com/photo/2020/03/04/07/28/black-4900736_960_720.jpg'
// };
if (selectedproductIndex == -1) {
addproduct(_formData['title'], _formData['description'],
_formData['image'], _formData['price'])
.then((_) => Navigator.pushReplacementNamed(context, '/products')
.then((_) => setSelectedProduct(null)));
} else {
updateproduct(_formData['title'], _formData['description'],
_formData['image'], _formData['price'])
.then((_) => Navigator.pushReplacementNamed(context, '/products')
.then((_) => setSelectedProduct(null)));
}
}
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<MainModel>(
builder: (BuildContext context, Widget child, MainModel model) {
final Widget pageContent =
_buildPageContent(context, model.selectedproduct);
return model.selectedProductIndex == -1
? pageContent
: Scaffold(
appBar: AppBar(
title: Text('Edit Product'),
),
body: pageContent,
);
},
);
}
}
================ そして、「_submitform」機能ブロックでエラーが発生しました。これは、ダイアログ表示ウィジェット で引数コンテキストを入力していたためです。これはエラーのキャプチャであり 、ここに関数があります、どうもありがとうございました、よろしくお願いします
_submitform
void _submitform(
Function addproduct, Function updateproduct, Function setSelectedProduct,
[int selectedproductIndex]) {
if (!_formkey.currentState.validate()) {
return;
}
_formkey.currentState.save();
//final Map<String, dynamic> product = {
// 'title': _title,
// 'description': _description,
// 'price': _price,
// 'image':
// 'https://cdn.pixabay.com/photo/2020/03/04/07/28/black-4900736_960_720.jpg'
// };
if (selectedproductIndex == -1) {
addproduct(_formData['title'], _formData['description'],
_formData['image'], _formData['price'])
.then((bool success) {
if (success) {
Navigator.pushReplacementNamed(context, '/products')
.then((_) => setSelectedProduct(null));
} else {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('something went wrong'),
content: Text('please try again!'),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('Okay'),
)
],
);
});
}
});
} else {
updateproduct(_formData['title'], _formData['description'],
_formData['image'], _formData['price'])
.then((_) => Navigator.pushReplacementNamed(context, '/products')
.then((_) => setSelectedProduct(null)));
}
}