Home > Back-end >  The argument type 'Function?' can't be assigned to the parameter type 'void Func
The argument type 'Function?' can't be assigned to the parameter type 'void Func

Time:10-30

Anyone knows a fix for this i keep getting this error, I used an OnPressed here, I think im suppose to use a VoidCallback here but when i use that i get the error:

"The argument type 'void Function(dynamic)' can't be assigned to the parameter type 'void Function()'."

Been stuck on this issue thanks for your help.

import 'package:concept1/constant.dart';
import 'package:concept1/screen/login/widget/welcome_back.dart';
import 'package:flutter/material.dart';

class LoginScreen extends StatelessWidget {
  const LoginScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(context),
      body: Column(
        children: [
          const WelcomeBack(),
          Container(
            padding: EdgeInsets.symmetric(vertical: 20, horizontal: 30),
            child: Column(children: <Widget>[
              InputTextField(
                label: 'Email',
                onChange: (value) {},
              ),
              InputTextField(
                label: 'Password',
                onChange: (value) {},
              ),
            ]),
          )
        ],
      ),
    );
  }

  AppBar buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: mBackgroundColor,
      elevation: 0,
      centerTitle: true,
      title: Text(
        'Login Screen',
        style: TextStyle(
          color: mPrimaryColor,
        ),
      ),
      leading: IconButton(
        icon: const Icon(Icons.arrow_back_ios),
        color: mPrimaryColor,
        onPressed: () {
          Navigator.pop(context);
        },
      ),
    );
  }
}

class InputTextField extends StatelessWidget {
  const InputTextField({
    Key? key,
    required this.label,
    required this.onChange,
  }) : super(key: key);

  final String label;
  final Function? onChange;

  @override
  Widget build(BuildContext context) {
    return TextField(
      onChanged: onChange,
      cursorColor: Colors.grey,
      decoration: InputDecoration(
          labelText: label,
          labelStyle: const TextStyle(color: Colors.grey),
          border: UnderlineInputBorder(
              borderSide: BorderSide(
            color: mPrimaryColor,
            width: 2,
          )),
          focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(
            color: mPrimaryColor,
            width: 2,
          )),
          enabledBorder: const UnderlineInputBorder(
              borderSide: BorderSide(
            color: Colors.grey,
            width: 0.5,
          ))),
    );
  }
}

CodePudding user response:

Whenever you pass a function as a argument you should use VoidCallBack instead of Function

Just change :

final Function? onChange

to

final VoidCallBack? onChange

CodePudding user response:

I figured it out!

    import 'package:concept1/constant.dart';
import 'package:concept1/screen/login/widget/welcome_back.dart';
import 'package:flutter/material.dart';

class LoginScreen extends StatelessWidget {
  const LoginScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(context),
      body: Column(
        children: [
          const WelcomeBack(),
          Container(
            padding: EdgeInsets.symmetric(vertical: 20, horizontal: 30),
            child: Column(children: <Widget>[
              InputTextField(
                label: 'Email',
                onChange: () {},
              ),
              InputTextField(
                label: 'Password',
                onChange: (() {}),
              )
            ]),
          )
        ],
      ),
    );
  }

  AppBar buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: mBackgroundColor,
      elevation: 0,
      centerTitle: true,
      title: Text(
        'Login Screen',
        style: TextStyle(
          color: mPrimaryColor,
        ),
      ),
      leading: IconButton(
        icon: const Icon(Icons.arrow_back_ios),
        color: mPrimaryColor,
        onPressed: () {
          Navigator.pop(context);
        },
      ),
    );
  }
}

class InputTextField extends StatelessWidget {
  const InputTextField({
    Key? key,
    required this.label,
    required this.onChange,
  }) : super(key: key);

  final String label;
  final VoidCallback onChange;

  @override
  Widget build(BuildContext context) {
    return TextField(
      onChanged: (value) {},
      cursorColor: Colors.grey,
      decoration: InputDecoration(
          labelText: label,
          labelStyle: const TextStyle(color: Colors.grey),
          border: UnderlineInputBorder(
              borderSide: BorderSide(
            color: mPrimaryColor,
            width: 2,
          )),
          focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(
            color: mPrimaryColor,
            width: 2,
          )),
          enabledBorder: const UnderlineInputBorder(
              borderSide: BorderSide(
            color: Colors.grey,
            width: 0.5,
          ))),
    );
  }
}
  • Related