Flutter项目编译报错求助:解决Expected ','、参数类型不匹配及Getter未找到等问题
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
Mismatched Validator Function Type
YourdefaultFormFeildcomponent defined thevalidateparameter asFunction()?, but Flutter'sTextFormField.validatorexpects a function of typeString? 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.Incorrect Callback Assignment in
defaultButton
IndefaultButton, you wroteonPressed: function!()which immediately executes the function instead of passing it as a callback. This breaks the intended behavior of the button.Redundant Null Check in Login Validator
Your email validator hadif(value.isEmpty != null)—this is unnecessary becausevaluewas defined as a non-nullString, but it's better to align with Flutter's API by usingString?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!()withfunctionto pass the callback correctly instead of executing it immediately. - Validator Type: Updated
validateparameter type indefaultFormFeildto match Flutter's required signature, and removed the parentheses when assigning it toTextFormField.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

