Reputation: 627
I am making a basic shopping app. I am unable to figure out the mistake I made because my IDE didnt show any error
This is the error
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
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 _imageUrlController = TextEditingController();
final _imageUrlFocusNode = FocusNode();
final _form = GlobalKey<FormState>();
var _editedProduct = Product( id: '', title: '', price: 0, description: '', imageUrl: '', );
var _initvalues = {
'title': '',
'description': '',
'price': '',
'imageUrl': ''
};
var _isInit = true;
var _isLoading = false;
@override
void initState() {
_imageUrlFocusNode.addListener(_updateImageUrl);
super.initState();
}
I guess this is where it wrong but my IDE didnt show any though.
@override
void didChangeDependencies() {
if (_isInit) {
final productId = ModalRoute.of(context)!.settings.arguments as String;
if (productId .isNotEmpty) {
_editedProduct = Provider.of(context)<Products>(context, listen: false);
_initvalues = {
'title': _editedProduct.title,
'description': _editedProduct.description,
'price': _editedProduct.price.toString(),
// 'imageurl': _editedProduct.imageUrl,
'imageUrl': ''
};
_imageUrlController.text = _editedProduct.imageUrl;
}
}
_isInit = false;
super.didChangeDependencies();
}
Upvotes: 3
Views: 16546
Reputation: 56
This is how I fixed it in my project:
When pushing the named route on tapping the + icon button in the manage your products screen, add an empty arguments string like this:
actions: [
IconButton(
onPressed: () {
Navigator.of(context).pushNamed(
EditProductScreen.routeName,
arguments: '',
);
},
icon: const Icon(Icons.add),
),
],
And by adding an if check to test for an empty id argument since, in order to add a new product, the id is initially set to ''
in the EditProductScreen with this code:
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: '',
);
// More code ...
}
Then update your didChangeDependencies(){...}
to look like this:
@override
void didChangeDependencies() {
if (isInit) {
final productId = ModalRoute.of(context)!.settings.arguments as String;
if (productId.isNotEmpty) {
_editedProduct =
Provider.of<Products>(context, listen: false).findById(productId);
}
_initValues = {
'title': _editedProduct.title,
'description': _editedProduct.description,
'price': _editedProduct.price.toString(),
// 'imageUrl': _editedProduct.imageUrl,
'imageUrl': '',
};
_imageUrlController.text = _editedProduct.imageUrl;
}
isInit = false;
super.didChangeDependencies();
}
Edit: Note that because we now push an empty argument String, the code that validates and saves our product will require an update as tapping the save button makes the program to run funny saving empty products and skipping the validation
Upvotes: 4
Reputation: 1
final productId = modulroute != null ? modulroute.settings.arguments : '';
then when and where you use productId
you use
productId.tostring();
Upvotes: 0
Reputation: 800
It seems that arguments
is null
final args = ModalRoute.of(context)!.settings.arguments;
if(args != null) {
final productId = argument.toString(); // Hoping only string will be there.
/// Rest of the code
}
Upvotes: 3
Reputation: 3305
Try by making a little change on your last snippet of code.
final productId = ModalRoute.of(context)!.settings.arguments.toString ?? "";
Upvotes: 2