我在main. dart中得到了这个错误,但我不认为它有什么问题。
Main. dart:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create:
(ctx) => Products(),
),
ChangeNotifierProvider(
create: (ctx) => Cart(),
),
ChangeNotifierProvider(
create: (ctx) =>
Orders(),
),
],
child: MaterialApp(
title: 'MyShop',
theme: ThemeData(
primarySwatch: Colors.purple,
accentColor: Colors.red,
fontFamily: 'Lato',
),
home: ProductsOverviewScreen(),
routes: {
ProductDetailsScreen.routeName: (ctx) => ProductDetailsScreen(),
CartScreen.routeName: (ctx) => CartScreen(),
OrdersScreen.routeName: (ctx) => OrdersScreen(),
UserProductsScreen.routeName: (ctx) => UserProductsScreen(),
EditProductScreen.routeName: (ctx) => EditProductScreen(),
},
),
);
}
}
我认为它是在编辑产品屏幕,当我点击添加产品的错误显示up.It工作正常之前,所以添加按钮没有任何问题:
编辑产品屏幕:
class EditProductScreen extends StatefulWidget {
static const routeName = '/edit-product';
@override
_EditProductScreenState createState() => _EditProductScreenState();
}
class _EditProductScreenState extends State<EditProductScreen> {
final _priceFocusNode = FocusNode(); //
final _descriptionFocusNode = FocusNode();
final _imageuRL = TextEditingController();
final _imageUrlFocusNode = FocusNode();
var _editedProduct =
Product(description: '', id: '', imageUrl: '', price: 0, title: '');
var isInit = true;
var _initValues = {
'title': '',
'description': '',
'price': '',
'imageURL': '',
};
final _form = GlobalKey<
FormState>();
@override
void initState() {
_imageUrlFocusNode.addListener(
_updateImageUrl);
super.initState();
}
@override
void didChangeDependencies() {
if (isInit) {
final productID = ModalRoute.of(context)!.settings.arguments as String;
if (productID != null) {
_editedProduct =
Provider.of<Products>(context, listen: false).findById(productID);
_initValues = {
'title': _editedProduct.title,
'description': _editedProduct.description,
'price': _editedProduct.price.toString(),
// 'imageUrl': _editedProduct.imageUrl,
};
_imageuRL.text = _editedProduct.imageUrl;
}
}
isInit = false;
super.didChangeDependencies();
}
@override
void dispose() {
//should be used with the focusnodes to avoid memory leak
_imageUrlFocusNode.removeListener(_updateImageUrl);
_priceFocusNode.dispose();
_descriptionFocusNode.dispose();
_imageuRL.dispose();
_imageUrlFocusNode.dispose();
super.dispose();
}
void _updateImageUrl() {
if (!_imageUrlFocusNode.hasFocus) {
setState(() {});
}
}
void _saveForm() {
final isValid =
_form.currentState!.validate();
if (!isValid) {
return;
}
_form.currentState!.save(); //this will save the form ;
if (_editedProduct.id != '') {
Provider.of<Products>(context, listen: false)
.updateProduct(_editedProduct.id, _editedProduct);
} else
Provider.of<Products>(context, listen: false).addProduct(
_editedProduct);
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Edit Title'), actions: [
IconButton(onPressed: _saveForm, icon: Icon(Icons.save_alt))
]),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _form,
child: ListView(
children: [
TextFormField(
initialValue: _initValues['title'],
decoration: InputDecoration(
labelText: 'Title',
),
textInputAction: TextInputAction.next,
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(
_priceFocusNode);
},
validator: (value) {
if (value!.isEmpty) {
return 'Please put in a value';
}
return null;
},
onSaved: (value) {
_editedProduct = Product(
description: _editedProduct.description,
id: _editedProduct.id,
imageUrl: _editedProduct.imageUrl,
price: _editedProduct.price,
isFavourite: _editedProduct.isFavourite,
title: value
.toString());
},
),
TextFormField(
initialValue: _initValues['price'],
decoration: InputDecoration(
labelText: 'Price',
),
textInputAction: TextInputAction.next,
keyboardType:
TextInputType.number,
focusNode: _priceFocusNode,
validator: (value) {
if (value!.isEmpty) {
return 'Please put in a value';
}
if (double.tryParse(value) == null) {
return "Please enter a valid number";
}
if (double.parse(value) <= 0) {
return "A number greater than 0";
}
return null;
},
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(
_descriptionFocusNode);
},
onSaved: (value) {
_editedProduct = Product(
description: _editedProduct.description,
id: _editedProduct.id,
imageUrl: _editedProduct.imageUrl,
price: double.parse(value.toString()),
isFavourite: _editedProduct.isFavourite,
title: _editedProduct
.title);
},
),
TextFormField(
initialValue: _initValues['description'],
decoration: InputDecoration(
labelText: 'Description',
),
maxLines:
3,
keyboardType: TextInputType.multiline,
focusNode: _descriptionFocusNode,
onSaved: (value) {
_editedProduct = Product(
description: value.toString(),
id: _editedProduct.id,
imageUrl: _editedProduct.imageUrl,
price: _editedProduct.price,
isFavourite: _editedProduct.isFavourite,
title: _editedProduct
.title);
},
validator: (value) {
if (value!.isEmpty) {
return 'Please put in a value';
}
if (value.length < 10) {
return 'should be atleast 10 char long';
}
return null;
},
),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
width: 100,
height: 100,
margin: EdgeInsets.only(top: 8, right: 10),
decoration: BoxDecoration(
border: Border.all(width: 1, color: Colors.grey)),
child: _imageuRL.text.isEmpty
? Text('Enter a Url')
: FittedBox(
child: Image.network(_imageuRL.text),
fit: BoxFit.cover,
),
),
Expanded(
child: TextFormField(
decoration: InputDecoration(labelText: 'Image Url'),
keyboardType: TextInputType.url,
textInputAction: TextInputAction.done,
focusNode: _imageUrlFocusNode,
onFieldSubmitted: (_) {
_saveForm();
},
onSaved: (value) {
_editedProduct = Product(
description: _editedProduct.description,
id: _editedProduct.id,
imageUrl: value.toString(),
price: _editedProduct.price,
isFavourite: _editedProduct.isFavourite,
title: _editedProduct
.title);
},
controller:
_imageuRL,
),
),
],
)
],
),
),
),
);
}
}
提前感谢!!因为我是新来的,所以我不明白如何解决它。
我不确定问题是什么,但如果让我猜,我会说这句话:
final productID = ModalRoute.of(context)!.settings.arguments as String;
是最有可能成为罪魁祸首的,如果是这种情况,希望这个特定的错误可以通过更改如下行来修复:
final productID = ModalRoute.of(context)!.settings.arguments as String?;
所以基本上我只是在String
之后添加了一个问号(?
),让dart知道该值可能是null
正如我所说,我不确定它是否能解决您的问题,但希望它能解决,如果它不能解决问题,那么显示错误发生在哪一行可能会对您有所帮助?