如何修复Flutter中“参数类型‘Icon’无法分配给参数类型‘String’”

问题描述 投票:0回答:2

我的 Flutter 代码中遇到以下错误:

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.

我尝试在 Flutter 应用程序中创建表单输入字段,但收到错误“参数类型‘Icon’无法分配给参数类型‘String’”。这是相关的代码片段:

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,
          ),
        )
      ],
    ),
    );
  }
}

截图:

我打算在“产品名称”标签旁边有一个带有

Icons.ac_unit
图标的输入字段。我该如何修复这个错误?

flutter forms dart widget
2个回答
1
投票

inputFieldWidget
构造函数需要一个字符串作为第二个位置参数。

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

您需要提供一个字符串形式的 keyName。您可以使用

prefixIcon
suffixIcon
命名参数来提供图标。

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,
),

更多关于

snippet_coder_utils


1
投票

FormHelper.inputFieldWidget( 语境, 常量图标(Icons.ac_unit), “产品名称”, ),

这里 inputFieldWidget 函数同时具有命名参数和位置参数。

注意事项:

  1. 命名参数序列可以是任何东西,同时调用带有名称参数的函数(这就是命名参数的美妙之处)

  2. 函数调用中的位置参数应保持与函数中定义的顺序相同。

  3. 如果函数或类构造函数同时具有命名参数和位置参数(是的,这是可能的,并且在上面的函数中使用了相同的参数),那么位置参数应该出现在命名参数之前,并且应该遵循规则 1 和 2。

如果您的情况顺序正确,但您已将 Icon() 分配给字符串类型的 keyName,则数据类型不匹配会导致问题。

© www.soinside.com 2019 - 2024. All rights reserved.