簡體   English   中英

錯誤:參數類型 'Future<dynamic> ' 不能分配給參數類型 'void Function()?'</dynamic>

[英]Error: The argument type 'Future<dynamic>' can't be assigned to the parameter type 'void Function()?'

在制作密碼重置頁面時,使用 Firebase Auth。 我用 Firebase 制作了幾個頁面,但從未遇到過這種錯誤,請提出解決方案。
它顯示錯誤:-

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(),

我做錯了什么?

代碼:

(我已經修剪了代碼,因為它不允許發布整個代碼)

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(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
  • 也許這里出了點問題
  Future<void> passwordReset() async {}
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MaterialButton(
        // onPressed: () => passwordReset(),
        onPressed: passwordReset,  
      ),
    );
  }

Material Button 和 flutter 中的所有按鈕只接受 void 函數。 那些只是開火而不返回任何東西的人。 在這種情況下,resetPassword 是一個 Future。 對於這種情況,您需要將其包裝在 void function () => resetPassword()中。 將此模式用於您聲明不無效的任何 function。 這是每個人偶爾都會遇到的一個非常普遍的錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM