提问者:小点点

参数类型“函数?”不能分配给参数类型“void Function(String)?”。此代码有修复方法吗?


任何人都知道解决这个问题的方法,我一直收到这个错误,我在这里使用了OnP的,我想我应该在这里使用VoidCallback,但当我使用它时,我得到了错误:

"参数类型'void Function(动态)'不能分配给参数类型'void Function()'。"

一直被困在这个问题上,感谢您的帮助。

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

共2个答案

匿名用户

每当您将函数作为参数传递时,您应该使用VoidCallBack而不是Function

只需更改:

final Function? onChange

final VoidCallBack? onChange

匿名用户

我想出来了!

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