Rex*_*Rex 5 flutter flutter-test flutter-dependencies flutter-web
我必须在一个屏幕上添加新产品以及编辑现有产品。所以我通过分配初始值来使用 didchangedependency 方法来更新屏幕,代码中出现了问题。请帮助我。所以错误是

错误显示在这里:
@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();
}
Run Code Online (Sandbox Code Playgroud)
在更改依赖项方法中,我尝试从一页获取参数,即产品 ID,但它显示为空。这是我已推送到编辑屏幕产品页面的屏幕截图,其中包含我试图获取此参数的参数。

这是为了推送添加产品页面

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);
},
),
),
],
),
],
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然实现了空安全,但大多数教程都是在它之前完成的,因此在您尝试检查返回ModalRoute值null是否返回值并根据该值执行操作的情况下,好的旧方法if(productId == null)现在不起作用。相反,你可以这样做:
final productId = ModalRoute.of(context)!.settings.arguments == null ? "NULL":ModalRoute.of(context)!.settings.arguments as String;
if(productId != "NULL"){
//Do stuff here
}
Run Code Online (Sandbox Code Playgroud)
如果ModalRoute给你null,你将字符串“NULL”分配给你的变量productId,否则你只需分配它返回的任何值。
然后您可以简单地检查 的值是否为productId并NULL进行相应操作。
| 归档时间: |
|
| 查看次数: |
26813 次 |
| 最近记录: |