'NoSuchMethodError: Closure call with mismatched arguments: function 'LoginScreen.build.<anonymous closure>' flutter

I recently started working with Getx and I seem to have a problem when passing functions as an argument from a custom widget so I manually created each button widget but left the CustomTextFormField widget and I get a red screen of death when excuting it. Everythig seems to work just fine when using the default TextFormField Widget so Idk what the issue is, Here is the code :


import 'package:flutter/material.dart';
import 'package:flutter_application_1/constance.dart';
import 'package:flutter_application_1/core/view_model/auth_view_model.dart';
import 'package:flutter_application_1/view/widgets/custom_text.dart';
import 'package:flutter_application_1/view/widgets/custom_text_form_field.dart';
import 'package:get/get.dart';

class LoginScreen extends GetWidget<AuthViewModel> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        backgroundColor: Colors.white,
      ),
      body: Padding(
        padding: EdgeInsets.only(top: 50, right: 20, left: 20),
        child: Form(
          key: _formKey,
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  CustomText(
                    text: "Welcome",
                    fontSize: 30,
                    color: Colors.black,
                  ),
                  CustomText(
                    text: "Sign up",
                    fontSize: 18,
                    color: primaryColor,
                  ),
                ],
              ),
              CustomText(
                text: "Sign in to continue",
                fontSize: 12,
                color: Colors.grey,
              ),
              const SizedBox(height: 30),
            //     TextFormField(
            //     onSaved:(value) {
            //       // controller.email = value;
            //     },
            // validator:(value) {
            //       // if (value == null) {
            //       //   print("Please insert E-mail");
            //       // }
            //     },),
              CustomTextFormField(
                text: "E-mail",
                hint: "Please insert your e-mail address",
                obscureText: false,
                onSave: (value) {
                  // controller.email = value;
                },
                validator: (value) {
                  // if (value == null) {
                  //   print("Please insert E-mail");
                  // }
                },
              ),
              SizedBox(
                height: 30,
              ),
              CustomTextFormField(
                text: "Password",
                hint: "*********",
                obscureText: true,
                onSave: (value) {
                  // controller.password = value;
                },
                validator: (value) {
                  // if (value == null) {
                  //   print("please insert password");
                  // }
                },
              ),
              SizedBox(
                height: 20,
              ),
              CustomText(
                text: "Forgot password ?",
                alignment: Alignment.topRight,
                fontSize: 14,
                color: Colors.black,
              ),
              SizedBox(
                height: 20,
              ),
              FlatButton(
                onPressed: () {
                  // formKey.currentState?.save();
                  // // if(formKey.currentState?.validate()){
                  // // controller.signInWithEmailAndPassword();
                  // // }
                },
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
                padding: EdgeInsets.all(10),
                color: primaryColor,
                child: CustomText(
                  text: "Sign In",
                  alignment: Alignment.center,
                  fontSize: 20,
                  color: Colors.white,
                ),
              ),
              SizedBox(
                height: 30,
              ),
              CustomText(
                text: "-OR-",
                alignment: Alignment.center,
              ),
              SizedBox(
                height: 30,
              ),
              Container(
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12),
                    color: Colors.grey.shade200),
                child: TextButton(
                  onPressed: () {
                    controller.googleSignInMethod();
                  },
                  //shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                  child: Row(
                    children: [
                      Container(
                        height: 30,
                        width: 30,
                        child: Image.asset('assets/images/google.png'),
                      ),
                      SizedBox(
                        width: 100,
                      ),
                      CustomText(
                        text: "Sign in with Google",
                      )
                    ],
                  ),
                ),
              ),
              SizedBox(
                height: 10,
              ),
              Container(
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12),
                    color: Colors.grey.shade200),
                child: TextButton(
                  onPressed: () {
                    print("kaka");
                  },
                  //shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                  child: Row(
                    children: [
                      Container(
                        height: 30,
                        width: 30,
                        child: Image.asset('assets/images/facebook.png'),
                      ),
                      SizedBox(
                        width: 100,
                      ),
                      CustomText(
                        text: "Sign in with Facebook",
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

This is the problematic widget :

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

class CustomTextFormField extends StatelessWidget {
  final String text;
  final String hint;
  final Function onSave;
  final Function validator;
  final bool obscureText;

  CustomTextFormField({
    required this.text,
    required this.hint,
    required this.onSave,
    required this.validator,
    required this.obscureText,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          CustomText(
            text: text,
            fontSize: 14,
            color: Colors.grey.shade900,
          ),
          TextFormField(
            onSaved: onSave(),
            validator: validator(),
            obscureText: obscureText,
            decoration: InputDecoration(
              hintText: hint,
              hintStyle: TextStyle(color: Colors.grey),
              fillColor: Colors.white,
            ),
          )
        ],
      ),
    );
  }
}

and here is the stack :

#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
#1      _objectNoSuchMethod (dart:core-patch/object_patch.dart:85:9)
#2      CustomTextFormField.build (package:flutter_application_1/view/widgets/custom_text_form_field.dart:30:28)
#3      StatelessElement.build (package:flutter/src/widgets/framework.dart:4827:28)
#4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#5      Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#6      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735:5)
#7      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729:5)
#8      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#9      MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36)
#10     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32)
...     Normal element mounting (44 frames)
#54     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#55     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36)
#56     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32)
...     Normal element mounting (85 frames)
#141    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#142    Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
#143    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#144    GetWidgetCacheElement.performRebuild (package:get/get_state_manager/src/simple/get_widget_cache.dart:35:11)
#145    Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#146    ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735:5)
#147    ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729:5)
#148    GetWidgetCacheElement.mount (package:get/get_state_manager/src/simple/get_widget_cache.dart:25:11)
...     Normal element mounting (19 frames)
#167    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#168    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36)
#169    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32)
...     Normal element mounting (255 frames)
#424    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#425    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6422:36)
#426    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6433:32)
...     Normal element mounting (388 frames)
#814    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#815    Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
#816    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1198:16)
#817    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1167:5)
#818    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1112:18)
#819    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2600:19)
#820    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13)
#821    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:944:7)
#822    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:924:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)

Thanks in advance



Solution 1:[1]

You need to change the type of onSave and validator property and pass the reference without calling it as follows:

class CustomTextFormField {
     ............
     final void Function(String?) onSave;
     final String? Function(String?) validator;
     ............

    .............
    TextFormField(
        onSaved: onSave,
        validator: validator,
    .............

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 S. M. JAHANGIR