我需要在一个屏幕上添加新产品并编辑现有的产品。所以我使用了didchangedependencies方法,通过分配初始值来更新屏幕,在我的代码中出了点问题,请帮帮我。错误如下:
![enter image description here](https://istack.dev59.com/kEodw.webp)
错误显示在这里:
@override
void didChangeDependencies() {
if (_isinit) {
final productId = ModalRoute.of(context)!.settings.arguments as String;
// ignore: unnecessary_null_comparison
if (productId != null) {
_editedproduct =
Provider.of<Products>(context, listen: false).FindByID(productId);
_imageUrlController.text = _editedproduct.imageUrl;
_initValues = {
'title': _editedproduct.title,
'description': _editedproduct.description,
'Price': _editedproduct.price.toString(),
// 'imageUrl': _editedproduct.imageUrl,
'imageUrl': '',
};
}
}
_isinit = false;
super.didChangeDependencies();
}
在 didChangeDependencies 方法中,我尝试从一个页面获取参数,即产品 ID,但它显示为空。这是一个屏幕截图,我已经将其推送到编辑产品页面,并尝试获取这些参数。
![enter image description here](https://istack.dev59.com/sg86J.webp)
这是用于推送添加产品页面的内容。
![enter image description here](https://istack.dev59.com/bfunV.webp)
import 'dart:html';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shoppingapp/Providers/Product.dart';
import 'package:shoppingapp/Providers/Products.dart';
class EditProductScreen extends StatefulWidget {
static const routeName = '/edit-products';
@override
_EditProductScreenState createState() => _EditProductScreenState();
}
class _EditProductScreenState extends State<EditProductScreen> {
final _priceFocusNode = FocusNode();
final _descriptionFocusNode = FocusNode();
final _imageUrlController = TextEditingController();
final _imageUrlFocusNode = FocusNode();
final _form = GlobalKey<FormState>();
var _editedproduct =
Product(id: '', title: '', description: '', price: 0, imageUrl: '');
var _isinit = true;
var _initValues = {
'title': '',
'description': '',
'price': '',
'imageUrl': '',
};
@override
void initState() {
_imageUrlFocusNode.addListener(_updateImageUrl);
super.initState();
}
@override
void didChangeDependencies() {
if (_isinit) {
final productId = ModalRoute.of(context)!.settings.arguments as String;
// ignore: unnecessary_null_comparison
if (productId != null) {
_editedproduct =
Provider.of<Products>(context, listen: false).FindByID(productId);
_imageUrlController.text = _editedproduct.imageUrl;
_initValues = {
'title': _editedproduct.title,
'description': _editedproduct.description,
'Price': _editedproduct.price.toString(),
// 'imageUrl': _editedproduct.imageUrl,
'imageUrl': '',
};
}
}
_isinit = false;
super.didChangeDependencies();
}
@override
void dispose() {
_imageUrlFocusNode.removeListener(_updateImageUrl);
_priceFocusNode.dispose();
_descriptionFocusNode.dispose();
_imageUrlFocusNode.dispose();
super.dispose();
}
void _updateImageUrl() {
if (!_imageUrlFocusNode.hasFocus) {
setState(() {});
}
}
void _saveForm() {
final isValid = _form.currentState!.validate();
if (isValid) {
return;
}
_form.currentState!.save();
if (_editedproduct.id != null) {
Provider.of<Products>(context, listen: false)
.updateProducts(_editedproduct.id, _editedproduct);
} else {
Provider.of<Products>(context, listen: false).addProducts(_editedproduct);
}
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Edit Product'),
actions: [
IconButton(
icon: Icon(Icons.save),
onPressed: _saveForm,
),
],
),
body: Padding(
padding: const EdgeInsets.all(15.0),
child: Form(
key: _form,
child: ListView(
children: [
TextFormField(
decoration: InputDecoration(
labelText: 'Title',
),
initialValue: _initValues['title'],
textInputAction: TextInputAction.next,
validator: (value) {
if (value!.isEmpty) {
return 'Please provide a value.';
}
return null;
},
onFieldSubmitted: (_) {
FocusScope.of(context).requestFocus(_priceFocusNode);
},
onSaved: (value) {
_editedproduct = Product(
title: value as String,
price: _editedproduct.price,
description: _editedproduct.description,
imageUrl: _editedproduct.imageUrl,
id: _editedproduct.id,
isFavourite: _editedproduct.isFavourite);
},
),
TextFormField(
initialValue: _initValues['price'],
decoration: InputDecoration(
labelText: 'Price',
),
textInputAction: TextInputAction.next,
keyboardType: TextInputType.number,
focusNode: _priceFocusNode,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter a price ';
}
if (double.tryParse(value) == null) {
return 'Please Enter a Valid Number';
}
if (double.parse(value) <= 0) {
return 'Please Enter the number greather no than zero';
}
},
onSaved: (value) {
_editedproduct = Product(
title: _editedproduct.title,
price: double.parse(value!),
description: _editedproduct.description,
imageUrl: _editedproduct.imageUrl,
id: _editedproduct.id,
isFavourite: _editedproduct.isFavourite);
},
),
TextFormField(
decoration: InputDecoration(
labelText: 'Description',
),
initialValue: _initValues['description'],
maxLines: 3,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.multiline,
focusNode: _descriptionFocusNode,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter a description ';
}
if (value.length < 10) {
return 'Should be at least 10 characters long.';
}
return null;
},
onSaved: (value) {
_editedproduct = Product(
title: _editedproduct.title,
price: _editedproduct.price,
description: value as String,
imageUrl: _editedproduct.imageUrl,
id: _editedproduct.id,
isFavourite: _editedproduct.isFavourite);
},
),
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: _imageUrlController.text.isEmpty
? Text('Enter a URL')
: FittedBox(
child: Image.network(
_imageUrlController.text,
fit: BoxFit.cover,
),
),
),
Expanded(
child: TextFormField(
decoration: InputDecoration(labelText: 'Image URl'),
keyboardType: TextInputType.url,
textInputAction: TextInputAction.done,
controller: _imageUrlController,
focusNode: _imageUrlFocusNode,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter a URL ';
}
if (!value.startsWith('http') &&
!value.startsWith('https')) {
return 'Please Enter a valid URL';
}
if (!value.endsWith('.png') &&
!value.endsWith('.jpg') &&
!value.endsWith('.jpeg')) {
return 'Please enter a valid image URL';
}
return null;
},
onFieldSubmitted: (_) {
_saveForm();
},
onSaved: (value) {
_editedproduct = Product(
title: _editedproduct.title,
price: _editedproduct.price,
description: _editedproduct.description,
imageUrl: value as String,
id: _editedproduct.id,
isFavourite: _editedproduct.isFavourite);
},
),
),
],
),
],
),
),
),
);
}
}