skip to Main Content

While making the Password Reset page, using Firebase Auth. I have made a few pages using Firebase but never encountered this kind of error, please suggest a solution.
It shows an error:-

Error: The argument type 'Future<dynamic>' can't be assigned to the parameter type 'void Function()?'.
lib/pages/forgot_pw_page.dart:82
 - 'Future' is from 'dart:async'.
                  onPressed: passwordReset(),

What I am doing wrong?

Code:

(I have trimmed the code, as it was not allowing to post whole)

class ForgotPasswordPage extends StatefulWidget {
  const ForgotPasswordPage({super.key});

  @override
  State<ForgotPasswordPage> createState() => _ForgotPasswordPageState();
}

class _ForgotPasswordPageState extends State<ForgotPasswordPage> {
  final _emailController = TextEditingController();

  @override
  void dispose() {
    _emailController.dispose();
    super.dispose();
  }

  Future passwordReset() async {
    await FirebaseAuth.instance.sendPasswordResetEmail(email: _emailController.text.trim());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[300],
      body: SafeArea(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 25),
                child: MaterialButton(
                  onPressed: passwordReset(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

2

Answers


    • Maybe something is wrong here
      Future<void> passwordReset() async {}
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: MaterialButton(
            // onPressed: () => passwordReset(),
            onPressed: passwordReset,  
          ),
        );
      }
    
    Login or Signup to reply.
  1. Material Button and all buttons in flutter at that only accept void functions. Those that just fire and don’t return anything. In this case, resetPassword is a Future. For this case, you’ll want to wrap it in a void function () => resetPassword(). Use this pattern for any function you declare that is not void. It’s a really common error everyone faces occasionally

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search