You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成Firebase登录出现日志提示问题求助

Flutter Firebase登录日志问题解析

作为编程新手,我在用Flutter开发Firebase登录页面时,输入邮箱和密码后控制台出现以下日志:

W/System (30113): Ignoring header X-Firebase-Locale because its value was null.
D/FirebaseAuth(30113): Notifying id token listeners about user ( gZ4NEwxh0qRRl32ypTGvqgX6kJx2 ).

我已经完成了应用与Firebase的关联,配置了邮箱认证方式,也添加了SHA1和SHA-256,相关代码如下:

主入口代码

Future<void> main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  get crossAxisAlignment => crossAxisAlignment;


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,

      home: FirstPage(),



   );
  }
}

状态监听页面代码

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

import 'package:untitled/connexion.dart';
import 'package:untitled/home.dart';



class MainPage extends StatelessWidget{
  const MainPage({Key? key}) : super(key: key);





  Widget build(BuildContext context){



  return Scaffold(
    body:StreamBuilder<User?>(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (context, snapshot) {
          if (snapshot.hasData){
          return HomePage();
        } else{
          return LloginScreen1();
        }

      }
    )


  );
 }
}

登录页面代码

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:delayed_display/delayed_display.dart';

import 'services/auth.dart';








class LloginScreen1 extends StatefulWidget {
  const LloginScreen1({Key? key}) : super(key: key);

  @override
  State<LloginScreen1> createState() => _LloginScreen1();
}

class _LloginScreen1 extends State<LloginScreen1> {

  Authservices auth = Authservices();


    final _emailController = TextEditingController();
    final _passwordController = TextEditingController();
    final _user = FirebaseAuth.instance.idTokenChanges();




    Future signIn() async{
      await FirebaseAuth.instance.signInWithEmailAndPassword(
          email: _emailController.text.trim(),
          password: _passwordController.text.trim(),

      );
    }



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



    @override
    Widget build(BuildContext context) {


    return Scaffold(



        body: SingleChildScrollView(
            child: Center(
                child: DelayedDisplay(
                    delay: Duration(milliseconds: 5),
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          SizedBox(height: 100),
                          Image.asset(
                            "images/nutrisport.png", width: 300, height: 200,),
                          SizedBox(width: 330,
                              child: TextField(
                                controller: _emailController,

                                style: TextStyle(fontSize: 20.0),
                                decoration: InputDecoration(
                                  prefixIcon: Icon(Icons.email_outlined),
                                    contentPadding: EdgeInsets.fromLTRB(
                                        20.0, 15.0, 20.0, 15.0),
                                    hintText: "Email",
                                    border:
                                    OutlineInputBorder(
                                        borderRadius: BorderRadius.circular(
                                            32.0))),


                              )
                          ),
                          SizedBox(height: 10,),

                          SizedBox(width: 330,
                              child: TextField(
                                obscureText: true,
                                controller: _passwordController,


                                style: TextStyle(fontSize: 20.0),
                                decoration: InputDecoration(
                                    suffixIcon: GestureDetector(
                                      child: Icon(
                                         Icons.visibility_off
                                      ),
                                    ),
                                    prefixIcon: Icon(Icons.lock_outline),
                                    contentPadding: EdgeInsets.fromLTRB(
                                        20.0, 15.0, 20.0, 15.0),
                                    hintText: "Password",
                                    border:
                                    OutlineInputBorder(
                                        borderRadius: BorderRadius.circular(
                                            32.0))),
                              ),
                          ),

                          SizedBox(height: 50,),


                          SizedBox(height:55,
                            width: 330,
                            child:ElevatedButton(
                              child: Text('Connexion',style: TextStyle(fontSize: 22),),
                              style: ButtonStyle(
                                  backgroundColor: MaterialStateProperty.all<Color>(Color.fromARGB(
                                      200, 119, 141, 67)),
                                  shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                                    RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(30),
                                    ),
                                  )
                              ),
                              onPressed: signIn,

                            ),),


                          TextButton(
                            onPressed: () {},
                            child: Text('Mot de passe oublié ?',
                              style: TextStyle(
                                  fontSize: 20,
                                  color: Colors.cyan
                              ),
                            ),
                          ),
                        ])))));
  }
}

日志问题说明

  1. W/System警告:这是Firebase SDK内部处理本地化时的无害警告,不会影响登录功能,完全可以忽略,不需要做任何处理。
  2. D/FirebaseAuth日志:这条是正常的成功提示,说明用户已经通过邮箱密码认证成功,Firebase正在通知所有ID token监听器用户状态已更新,你的MainPage里的StreamBuilder会接收到这个状态变化,自动跳转到HomePage,这是预期的正常行为。

代码优化建议

  1. 添加登录错误处理:当前signIn方法没有捕获异常,用户输入错误邮箱/密码、网络异常时会直接崩溃,建议修改为:
Future signIn() async{
  try {
    await FirebaseAuth.instance.signInWithEmailAndPassword(
      email: _emailController.text.trim(),
      password: _passwordController.text.trim(),
    );
  } on FirebaseAuthException catch (e) {
    // 弹出错误提示给用户
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(e.message ?? '登录失败')),
    );
  }
}
  1. 实现密码可见性切换:当前密码输入框的眼睛图标点击没反应,可以添加状态变量实现切换:
bool _obscurePassword = true;

// 在TextField的suffixIcon里修改
suffixIcon: GestureDetector(
  onTap: () {
    setState(() {
      _obscurePassword = !_obscurePassword;
    });
  },
  child: Icon(
    _obscurePassword ? Icons.visibility_off : Icons.visibility
  ),
),
// 同时把TextField的obscureText改成_obscurePassword
obscureText: _obscurePassword,
  1. 主入口页面修正:MyApp的home是FirstPage(),但你提供的代码里没有这个页面的定义,应该改成MainPage()才能正常触发登录状态监听。

内容的提问来源于stack exchange,提问作者Rida Maiquez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 20:45:39