Flutter Toast 消息在 Flutter 中无法正常工作

问题描述 投票:0回答:0

我的 Flutter Toast 消息不能正常工作,所以只是为了测试 flutter toast 消息是否有错误或异常,我写了同样的电子邮件来注册,它已经在我的 Firebase 中注册了。但是当我按下注册按钮时,它在 VSCode 中显示了一个错误,而不是在前端使用 flutter toast 消息显示我。

(Shown in Picture 1)

但是只要我按下 VSCode 上的

Hot Restart
按钮,它就会开始在前端显示 flutter toast 消息几秒钟。

这里的问题是,只有在我按下

Hot Restart
按钮后才会出现toast消息,否则toast消息永远不会出现。
(Shown in Picture 2)

The way error appears in VSCode:

Toast Message appearing after I press Hot Restart:

signup_screen.dart

import 'package:firebase/ui/auth/login_screen.dart';
import 'package:firebase/widgets/round_button.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_flushbar/flutter_flushbar.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../../utils/utils.dart';

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

  @override
  State<SignupScreen> createState() => _SignupScreenState();
}

class _SignupScreenState extends State<SignupScreen> {
  bool loading = false;
  final _formKey = GlobalKey<FormState>();
  final emailController = TextEditingController();
  final passwordController = TextEditingController();

  // for Firebase Authentication
  FirebaseAuth _auth = FirebaseAuth.instance;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    // when the screen is gone, then email and password should also be removed/disposed
    emailController.dispose();
    passwordController.dispose();
  }

  void signUp() {
    setState(() {
      loading = true;
    });
    // this is the code to register user Firebase Authentication
    _auth
        .createUserWithEmailAndPassword(
      email: emailController.text.toString(),
      password: passwordController.text.toString(),
    )
        .then((value) {
      setState(() {
        loading = false;
      });
    }).onError((error, StackTrace) {
      Utils.toastMessage(error.toString());
      // Fluttertoast.showToast(msg: error.toString());

      setState(() {
        loading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        automaticallyImplyLeading: false, // to remove backbutton
        title: const Text(
          "Sign-up Screen",
          style: TextStyle(fontSize: 20),
        ),
        // backgroundColor: Colors.indigo,
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 50, vertical: 20),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Form(
              key: _formKey,
              child: Column(
                children: [
                  TextFormField(
                    keyboardType: TextInputType.emailAddress,
                    controller: emailController,
                    decoration: const InputDecoration(
                      hintText: "Email",
                      //  helperText: "Please enter your email",
                      helperStyle: TextStyle(fontSize: 15),
                      prefixIcon: Icon(Icons.alternate_email),
                    ),
                    validator: (value) {
                      if (value!.isEmpty) {
                        return "Enter email";
                      } else {
                        return null;
                      }
                    },
                  ),
                  const SizedBox(
                    height: 20,
                  ),
                  TextFormField(
                    obscureText: true,
                    controller: passwordController,
                    decoration: const InputDecoration(
                      hintText: "Password",
                      // helperText: "Please enter your password",
                      helperStyle: TextStyle(fontSize: 15),
                      prefixIcon: Icon(Icons.lock_outline_rounded),
                    ),
                    validator: (value) {
                      if (value!.isEmpty) {
                        return "Enter password";
                      } else {
                        return null;
                      }
                    },
                  ),
                ],
              ),
            ),
            const SizedBox(
              height: 40,
            ),
            RoundButton(
              title: "Sign up",
              loading: loading,
              onTap: () {
                if (_formKey.currentState!.validate()) {
                  signUp(); // calling function made in start
                }
              },
            ),
            const SizedBox(
              height: 30,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              // crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                const Text(
                  "Already have an account?",
                  style: TextStyle(fontSize: 16),
                ),
                TextButton(
                    onPressed: () {
                      Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: ((context) => const LoginScreen())));
                    },
                    child:
                        const Text("Login now", style: TextStyle(fontSize: 16)))
              ],
            )
          ],
        ),
      ),
    );
  }
}

utils.dart

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

class Utils {
  static void toastMessage(String message) {
    Fluttertoast.showToast(
      msg: message,
      toastLength: Toast.LENGTH_LONG,
      gravity: ToastGravity.CENTER,
      backgroundColor: Colors.red,
      textColor: Colors.white,
      fontSize: 16.0,
      timeInSecForIosWeb: 1,
    );
  }
}
flutter dart exception error-handling toast
© www.soinside.com 2019 - 2024. All rights reserved.