Search code examples
flutterformsdartwidget

How to fix "The argument type 'Icon' can't be assigned to the parameter type 'String'" in Flutter


I'm encountering the following error in my Flutter code:

Performing hot restart... Syncing files to device sdk gphone64 x86 64... 
lib/product_add_edit.dart:48:19: Error: The argument type 'Icon' can't be assigned to the parameter type 'String'.
'Icon' is from 'package:flutter/src/widgets/icon.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/icon.dart'). 
        const Icon(Icons.ac_unit),
        ^ Restarted application in 867ms.

I'm trying to create a form input field in my Flutter app and I'm getting the error "The argument type 'Icon' can't be assigned to the parameter type 'String'". Here's the relevant code snippet:

class ProductAddEdit extends StatefulWidget {
  const ProductAddEdit({Key? key}) : super(key: key);

  @override
  State<ProductAddEdit> createState() => _ProductAddEditState();
}

class _ProductAddEditState extends State<ProductAddEdit> {
  static final GlobalKey<FormState> globalKey = GlobalKey<FormState>();
  bool isAPICallProcess = false;
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
          appBar: AppBar(
            title: const Text("NodeJS - CRUD App"),
            elevation: 0,
          ),
          backgroundColor: Colors.grey[200],
          body: ProgressHUD(
            child: Form(
              key: globalKey,
              child: Text("be like Elon Musk to be successfull"),
            ),
            inAsyncCall: isAPICallProcess,
            opacity: .3,
            key: UniqueKey(),
          ),
        ));
  }
  Widget productForm(){
    return SingleChildScrollView(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(bottom: 10, top: 10),
          child: FormHelper.inputFieldWidget(
              context,
            const Icon(Icons.ac_unit),
            "Product Name",
              (onValidateVal) {
                if(onValidateVal.isEmpty) {
                  return "Product name can't be empty";
                }
                return null;
              },
              (onSavedVal) {},
            borderColor: Colors.black,
            borderFocusColor: Colors.black,
            textColor: Colors.black,
            hintColor: Colors.black.withOpacity(.7),
            borderRadius: 10,
            showPrefixIcon: false,
          ),
        )
      ],
    ),
    );
  }
}

Screenshots:

enter image description here

enter image description here

I intend to have an input field with the Icons.ac_unit icon next to the "Product Name" label. How can I fix this error?


Solution

  • inputFieldWidget constructor expect a string as second position argument.

    static Widget inputFieldWidget(
        BuildContext context,
        String keyName, // name
    

    You need to provide a keyName as string. You can use prefixIcon or suffixIcon named arguments to provide icon.

    child: FormHelper.inputFieldWidget(
      context,
      "MyKey",
      "Product Name",
      prefixIcon: Icon(Icons.ac_unit),
      (onValidateVal) {
        if (onValidateVal.isEmpty) {
          return "Product name can't be empty";
        }
        return null;
      },
      (onSavedVal) {},
      borderColor: Colors.black,
      borderFocusColor: Colors.black,
      textColor: Colors.black,
      hintColor: Colors.black.withOpacity(.7),
      borderRadius: 10,
      showPrefixIcon: false,
    ),
    

    More about snippet_coder_utils