EN VI

Flutter - The argument type 'Null' can not be assigned to the parameter type 'String'. (argument_type_not_assignable)?

2024-03-14 15:30:04
Flutter - The argument type 'Null' can not be assigned to the parameter type 'String'. (argument_type_not_assignable)

I've got two issues here: number one is for id:null, The argument type 'Null' can't be assigned to the parameter type 'String'.dart

number two is title: value, The argument type 'String?' can't be assigned to the parameter type 'String'.dart

import 'package:flutter/material.dart';
import 'package:my_shop/providers/product.dart';

class EditProductScreen extends StatefulWidget {
static const routeName = '/edit-product';

@override
State<EditProductScreen> createState() => _EditProductScreenState();
}

class _EditProductScreenState extends State<EditProductScreen> {
final _priceFocusNode = FocusNode();
final _descriptionFocusNode = FocusNode();
final _imageUrlFocusNode = FocusNode();
final _imageUrlController = TextEditingController();
final _form = GlobalKey<FormState>();
var _editedProduct =
  Product(id: null, title: '', description: '', price: 0.0, imageUrl: '');

  @override
  void initState() {
  _imageUrlFocusNode.addListener(_updateImageUrl);
  super.initState();
  }

@override
void dispose() {
_imageUrlFocusNode.removeListener(_updateImageUrl);
_priceFocusNode.dispose();
_descriptionFocusNode.dispose();
_imageUrlFocusNode.dispose();
_imageUrlController.dispose();
super.dispose();
}

void _updateImageUrl() {
  if (_imageUrlFocusNode.hasFocus) {
    setState(() {});
   }
  }

void _saveForm() {
 _form.currentState!.save();
}

@override
Widget build(BuildContext context) {
 return Scaffold(
  appBar: AppBar(
    title: const Text('edit product'),
    actions: <Widget>[
      IconButton(
        onPressed: _saveForm,
        icon: const Icon(Icons.save),
      )
    ],
  ),
  body: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Form(
        key: _form,
        child: ListView(
          children: <Widget>[
            TextFormField(
              decoration: const InputDecoration(labelText: 'Title'),
              textInputAction: TextInputAction.next,
              onFieldSubmitted: (_) {
                FocusScope.of(context).requestFocus(_priceFocusNode);
              },
              onSaved: (value) {
                _editedProduct = Product(
                    id: null,
                    title: value,
                    description: _editedProduct.description,
                    price: _editedProduct.price,
                    imageUrl: _editedProduct.imageUrl);
              },
            ),
            TextFormField(
              decoration: const InputDecoration(labelText: 'Price'),
              textInputAction: TextInputAction.next,
              keyboardType: TextInputType.number,
              focusNode: _priceFocusNode,
              onFieldSubmitted: (_) {
                FocusScope.of(context).requestFocus(_descriptionFocusNode);
              },
              onSaved: (value) {
                _editedProduct = Product(
                    id: null,
                    title: _editedProduct.title,
                    description: _editedProduct.description,
                    price:double.parse(value),
                    imageUrl: _editedProduct.imageUrl);
              },
            ),
            TextFormField(
              decoration: const InputDecoration(labelText: 'Description'),
              maxLines: 3,
              textInputAction: TextInputAction.next,
              keyboardType: TextInputType.multiline,
              focusNode: _descriptionFocusNode,
              onSaved: (value) {
                _editedProduct = Product(
                    id: null,
                    title: _editedProduct.title,
                    description: value,
                    price: _editedProduct.price,
                    imageUrl: _editedProduct.imageUrl);
              },
            ),
            Row(
              children: <Widget>[
                Container(
                    width: 100,
                    height: 100,
                    margin: const EdgeInsets.only(top: 8, right: 10),
                    decoration: BoxDecoration(
                        border: Border.all(width: 1, color: Colors.grey)),
                    child: _imageUrlController.text.isEmpty
                        ? const Text('Enter a URL!')
                        : FittedBox(
                            child: Image.network(_imageUrlController.text),
                            fit: BoxFit.cover,
                          )),
                Expanded(
                  child: TextFormField(
                    decoration:
                        const InputDecoration(labelText: 'Image URL'),
                    keyboardType: TextInputType.url,
                    textInputAction: TextInputAction.done,
                    controller: _imageUrlController,
                    focusNode: _imageUrlFocusNode,
                    onSaved: (value) {
                      _editedProduct = Product(
                          id: null,
                          title: _editedProduct.title,
                          description: _editedProduct.description,
                          price: _editedProduct.price,
                          imageUrl: value);
                    },
                    onFieldSubmitted: (_) {
                      _saveForm();
                    },
                  ),
                ),
              ],
            )
          ],
        )),
  ),
);
}
}

Please help me out and resolve these issues. here are the screenshots: enter image description here

enter image description here

Solution:

First of all, you need to learn what the null safety is. If you create some variables with ?, that is nullable variable, so that variable can receive null. For example, if you create variable like this: String? id. It can receive null value.

You might use the example below:

class Product{
String? id
String? title
}
Product({required this.id, required this.title});

Secondly, if you are giving value to your variable that is not nullable, you can use ?? and give default value in case the value is null. For example, _editedProduct = Product( id:null, title: value??"" )

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login