You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter项目编译报错求助:解决Expected ','、参数类型不匹配及Getter未找到等问题

Fixing Your Flutter Compilation Errors

Hey there! Let's work through those compilation errors in your code—they're all related to incorrect function type definitions and improper callback handling. Here's the breakdown and fixed code:

Key Issues Identified

  1. Mismatched Validator Function Type
    Your defaultFormFeild component defined the validate parameter as Function()?, but Flutter's TextFormField.validator expects a function of type String? Function(String?) (takes an optional string input, returns an optional error message string). Additionally, you were calling the function (validate!()) instead of passing the function reference itself.

  2. Incorrect Callback Assignment in defaultButton
    In defaultButton, you wrote onPressed: function!() which immediately executes the function instead of passing it as a callback. This breaks the intended behavior of the button.

  3. Redundant Null Check in Login Validator
    Your email validator had if(value.isEmpty != null)—this is unnecessary because value was defined as a non-null String, but it's better to align with Flutter's API by using String? for the parameter.

Fixed Code

Updated components.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

Widget defaultButton(
    {double width = double.infinity,
    Color background = Colors.blue,
    double radius = 10.0,
    VoidCallback? function, // Use VoidCallback for clearer callback type
    required String text,
    bool isUpperCase = true}) =>
    Container(
      height: 40.0,
      width: width,
      child: MaterialButton(
          onLongPress: () {},
          onPressed: function, // Pass function reference directly (no parentheses)
          child: Text(
            isUpperCase ? text.toUpperCase() : text,
            style: TextStyle(color: Colors.white),
          )),
      decoration: BoxDecoration(
        borderRadius: BorderRadiusDirectional.circular(radius),
        color: background,
      ),
    );

Widget defaultFormFeild({
  required TextEditingController controller,
  required TextInputType type,
  void Function(String)? onSubmit,
  void Function(String)? onChange,
  required String? Function(String?)? validate, // Correct validator type
  required String label, // Explicit String type instead of var
  required IconData prefix,
}) =>
    TextFormField(
      controller: controller,
      keyboardType: type,
      onFieldSubmitted: onSubmit,
      onChanged: onChange,
      validator: validate, // Pass the function reference, don't call it
      decoration: InputDecoration(
        labelText: label,
        prefixIcon: Icon(prefix),
        border: OutlineInputBorder()
      ),
    );

Updated Login Screen defaultFormFeild Call

In login_screen.dart, modify the email field's validate parameter to match the corrected type:

defaultFormFeild(
  controller: emailController,
  label: 'Email',
  prefix: Icons.email,
  type: TextInputType.emailAddress,
  validate: (String? value){ // Use String? to align with Flutter's API
    if(value == null || value.isEmpty){
      return 'Email Cannot Be Empty';
    }
    return null;
  }
),

What Changed?

  • Button Callback: Replaced function!() with function to pass the callback correctly instead of executing it immediately.
  • Validator Type: Updated validate parameter type in defaultFormFeild to match Flutter's required signature, and removed the parentheses when assigning it to TextFormField.validator.
  • Null Safety Alignment: Adjusted the login validator to use String? for the input parameter, making the null check more accurate and consistent with Flutter's standards.

After applying these changes, your code should compile successfully and work as expected!

内容的提问来源于stack exchange,提问作者Diaa Nassar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 21:49:08