类型'Null'不是类型大小写中类型'String'的子类型



此错误可能的原因,请指导。

这是我的代码

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/product.dart';
import '../provider/products.dart';
class EditProductScreen extends StatefulWidget {
static const routeName = '/edite-product-screen';
@override
State<EditProductScreen> createState() => _EditProductScreenState();
}
class _EditProductScreenState extends State<EditProductScreen> {
final priceFocusNode = FocusNode();
final descriptionFocusNode = FocusNode();
final imageURLController = TextEditingController();
// TextEditingController listens to updates in the associated TextField and alert its listeners of any changes
final imageURLFocusNode = FocusNode();
final keyForForm = GlobalKey<FormState>();
var emptyProduct =
Product(id: "", title: "", description: "", price: 0, imageUrl: "");
var isInit = true;
var initValues = {
'title': "",
'description': "",
'price': "",
'imageURL': "",
};
@override
void initState() {
super.initState();
imageURLFocusNode.addListener(updateImageUrl);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (isInit) {
final receivedProductID =
ModalRoute.of(context)?.settings.arguments as String;
if (receivedProductID != null) {
emptyProduct = Provider.of<Products>(context, listen: false)
.selectedProduct(receivedProductID);
initValues = {
'title': emptyProduct.title.toString(),
'description': emptyProduct.description.toString(),
'price': emptyProduct.price.toString(),
// 'imageURL': "",
};
imageURLController.text = emptyProduct.imageUrl as String;
}
}
isInit = false;
}
void updateImageUrl() {
if (!imageURLFocusNode.hasFocus) {
if ((!imageURLController.text.startsWith("http") &&
!imageURLController.text.startsWith("https")) ||
(!imageURLController.text.endsWith("jpg") &&
!imageURLController.text.endsWith("png") &&
!imageURLController.text.endsWith("jpeg")) ||
(imageURLController.text.isEmpty)) {
return;
}
setState(() {});
}
}
void saveTheProductBasedOnInformationOfTheForm() {
final validStatus = keyForForm.currentState?.validate();
if (!validStatus!) {
return;
}
keyForForm.currentState?.save();
if (emptyProduct.id != null) {
Provider.of<Products>(context, listen: false)
.updateProduct(emptyProduct.id as String, emptyProduct);
} else {
Provider.of<Products>(context, listen: false).addProduct(emptyProduct);
}
Navigator.of(context).pop();
}
@override
void dispose() {
imageURLFocusNode.removeListener(updateImageUrl);
priceFocusNode.dispose();
descriptionFocusNode.dispose();
imageURLController.dispose();
imageURLFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Products"),
actions: [
IconButton(
onPressed: saveTheProductBasedOnInformationOfTheForm,
icon: Icon(Icons.save))
],
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: keyForForm,
child: ListView(
children: [
TextFormField(
initialValue: initValues['title'],
validator: (value) {
if (value!.isEmpty) {
return "Please enter a valid value";
} else {
return null;
}
},
decoration: InputDecoration(labelText: "Title"),
textInputAction: TextInputAction.next,
onFieldSubmitted: (_) => {
FocusScope.of(context).requestFocus(priceFocusNode),
},
onSaved: (value) {
emptyProduct = Product(
id: emptyProduct.id,
title: value,
description: emptyProduct.description,
price: emptyProduct.price,
imageUrl: emptyProduct.imageUrl,
isFavorite: emptyProduct.isFavorite);
},
),
TextFormField(
initialValue: initValues['price'],
onSaved: (value) {
emptyProduct = Product(
id: emptyProduct.id,
title: emptyProduct.title,
description: emptyProduct.description,
price: double.parse(value as String),
imageUrl: emptyProduct.imageUrl,
isFavorite: emptyProduct.isFavorite);
},
decoration: InputDecoration(labelText: "Price"),
textInputAction: TextInputAction.next,
keyboardType: TextInputType.number,
focusNode: priceFocusNode,
onFieldSubmitted: (_) =>
FocusScope.of(context).requestFocus(descriptionFocusNode),
validator: (value) {
if (value!.isEmpty) {
return "Please enter a valid price";
}
if (double.tryParse(value) == null) {
return "Please enter a valid number";
}
if (double.parse(value) <= 0) {
return "Please enter a value greater than 0";
} else {
return null;
}
},
),
TextFormField(
initialValue: initValues['description'],
onSaved: (value) {
emptyProduct = Product(
id: emptyProduct.id,
title: emptyProduct.title,
description: value,
price: emptyProduct.price,
imageUrl: emptyProduct.imageUrl,
isFavorite: emptyProduct.isFavorite);
},
decoration: InputDecoration(labelText: "Description"),
keyboardType: TextInputType.multiline,
maxLines: 3,
validator: (value) {
if (value!.isEmpty) {
return "Please enter a valid description";
}
if (value.length < 10) {
return "Please enter more description";
} else {
return null;
}
},
),
Row(crossAxisAlignment: CrossAxisAlignment.end, children: [
Container(
width: 100,
height: 100,
margin: EdgeInsets.only(top: 10, right: 10),
decoration: BoxDecoration(
border: Border.all(width: 1, color: Colors.grey)),
child: imageURLController.text.isEmpty
? Text("Please enter URL")
: Image.network(imageURLController.text),
),
Expanded(
child: TextFormField(
initialValue: initValues['imageURL'],
validator: (value) {
if (value!.isEmpty) {
return "Please enter valid Image URL";
}
if ((!value.startsWith("http") &&
!value.startsWith("https")) ||
(!value.endsWith("jpg") &&
!value.endsWith("png") &&
!value.endsWith("jpeg"))) {
return "Please enter a valid URL!";
}
},
onSaved: (value) {
emptyProduct = Product(
id: emptyProduct.id,
title: emptyProduct.title,
description: emptyProduct.description,
price: emptyProduct.price,
imageUrl: value,
isFavorite: emptyProduct.isFavorite);
},
decoration: InputDecoration(labelText: "Image URL"),
keyboardType: TextInputType.url,
textInputAction: TextInputAction.done,
controller: imageURLController,
maxLines: 3,
focusNode: imageURLFocusNode,
onFieldSubmitted: (_) {
saveTheProductBasedOnInformationOfTheForm();
},
),
),
])
],
)),
),
);
}
}

当(receivedProductID != null)时屏幕打开没有错误,但当(receivedProductID == null)时显示以下错误;查看错误图片

当我尝试从屏幕返回时,我看到这个错误查看错误图片

此错误可能的原因是什么,请指导。

第一个错误的原因是类型强制转换:

final receivedProductID = ModalRoute.of(context)?.settings.arguments as String;

将可空值强制转换为String。应该是字符串?因为它可以为空。

final receivedProductID =
ModalRoute.of(context)?.settings.arguments as String?;

相关内容

  • 没有找到相关文章

最新更新