Undefined name 'firebase' Flutter
4,382
Solution 1
You need to initialize your Firebase into your main method
First flutter pub get
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(HomePage());
}
Solution 2
You have to have the firebase core package added
firebase_core: ^7.0.1 (check the actual package version) flutter pub get
and add the package to your code
import 'package:firebase_core/firebase_core.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(HomePage());
}
Solution 3
I fixed the issue by upgrading to a newer version of firebase_core
(I went with 1.0.0
)
Author by
Rishail Siddiqui
Updated on December 27, 2022Comments
-
Rishail Siddiqui over 1 year
Hello I am trying to implement firebase authentication in my flutter app I am getting this error Undefined name 'firebase'
Here is the code for reference:
import 'dart:math'; import 'package:flutter/cupertino.dart'; import 'package:multi_purpose_scope/Animation/FadeAnimation.dart'; import 'package:flutter/material.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_core/firebase_core.dart'; Future<void>main() async{ WidgetsFlutterBinding.ensureInitialized(); } class HomePage extends StatelessWidget { final FirebaseApp _fbApp=Firebase.initializeApp(); final _formKey = GlobalKey<FormState>(); TextEditingController _emailController = TextEditingController(); TextEditingController _passwordController = TextEditingController(); gotoSecondActivity(BuildContext context){ final mq=MediaQuery.of(context); Navigator.push( context, MaterialPageRoute(builder: (context) => SecondActivity()), ); } @override Widget build(BuildContext context) { TextEditingController _emailControllerField=TextEditingController(); TextEditingController _passwordControllerField=TextEditingController(); return Scaffold( backgroundColor: Colors.white, body: SingleChildScrollView( child: RaisedButton( onPressed: () async { gotoSecondActivity(context); try{ FirebaseUser user=(await FirebaseAuth.instance.signInWithEmailAndPassword( email: _emailController.text, password: _passwordController.text, )); if(user!=null){ Navigator.of(context).pushNamed(gotoSecondActivity(context).menu); } } catch(e){ print(e); _emailController.text=''; _passwordController.text=''; } }, child: Container( child: Column( children: <Widget>[ Container( height: 400, decoration: BoxDecoration( image: DecorationImage( image: AssetImage('assets/images/loginHeader.png'), fit: BoxFit.fill ) ), child: Stack( children: <Widget>[ ], ), ), Padding( padding: EdgeInsets.all(30.0), child: Column( children: <Widget>[ FadeAnimation(1.8, Container( padding: EdgeInsets.all(5), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(10), boxShadow: [ BoxShadow( color: Color.fromRGBO(143, 148, 251, .2), blurRadius: 20.0, offset: Offset(0, 10) ) ] ), child: Column( children: <Widget>[ Container( padding: EdgeInsets.all(8.0), decoration: BoxDecoration( border: Border(bottom: BorderSide( color: Colors.grey[100])) ), child: TextField( controller: _emailControllerField, keyboardType: TextInputType.emailAddress, decoration: InputDecoration( border: InputBorder.none, hintText: "Email or Phone number", hintStyle: TextStyle( color: Colors.grey[400]) ), ), ), Container( padding: EdgeInsets.all(8.0), child: TextField( obscureText: true, controller: _passwordControllerField, decoration: InputDecoration( border: InputBorder.none, hintText: "Password", hintStyle: TextStyle( color: Colors.grey[400]) ), ), ) ], ), )), SizedBox(height: 30,), FadeAnimation(2, Container( height: 50, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), gradient: LinearGradient( colors: [ Color.fromRGBO(214, 0, 27, 1), Color.fromRGBO(214, 0, 27, 1), ] ) ), child: Center( child: Text("Login", style: TextStyle( color: Colors.white, fontWeight: FontWeight.bold),), ), )), SizedBox(height: 70,), FadeAnimation(1.5, Text("Forgot Password?", style: TextStyle( color: Color.fromRGBO(214, 0, 27, 1)),)), ], ), ) ], ), ), ) ), ); } } class SecondActivity extends StatelessWidget { gotoRegister(BuildContext context){ Navigator.push( context, MaterialPageRoute(builder: (context) =>Register()), ); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Container( child: Container( height: 174, decoration: BoxDecoration( image: DecorationImage( image: AssetImage('assets/images/patient_list.png'), ) ), child: Stack( children: <Widget>[ Container( alignment: Alignment.center, height: 128, child: Text( 'Patient List', style: TextStyle( fontWeight: FontWeight.bold,fontSize: 30, color: Colors.white ), ), ) ], ), ), ), floatingActionButton: FloatingActionButton( onPressed: () { gotoRegister(context); }, tooltip: 'Increment', child: Icon(Icons.add), backgroundColor: Color.fromRGBO(214, 0, 27,1), ), ), ); } } class Register extends StatelessWidget { goBack(BuildContext context) { Navigator.pop(context); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: SafeArea( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 40), child: Column( children: <Widget>[ Stack( alignment: Alignment.center, children: <Widget>[ Image.asset('assets/images/patient_list.png'), Text('Registration',style: TextStyle(fontWeight:FontWeight.bold,fontSize: 30,color: Colors.white),) ] ), const SizedBox(height: 10), const SampleTextField(hintText: 'Enter Name'), const SizedBox(height:10), const SampleTextField(hintText: 'Enter MR-Number'), const SizedBox(height: 10), const SampleTextField(hintText: 'Enter Phone Number'), const SizedBox(height: 10), const SampleTextField(hintText: 'Enter Hospital Name'), const SizedBox(height:10), FlatButton( onPressed: () { }, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20), ), // materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, // visualDensity: VisualDensity.compact, padding: const EdgeInsets.symmetric(vertical: 20), color: Color.fromRGBO(214, 0, 27,1), child: const Center( child: Text('Register User'), ), ), ] ), ), ), ), ); } } class SampleTextField extends StatelessWidget { const SampleTextField({ this.controller, this.hintText = '', }); final TextEditingController controller; final String hintText; @override Widget build(BuildContext context) { return TextField( controller: controller, decoration: InputDecoration( border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: const BorderSide( width: 1, color: Colors.black54, ), ), hintText: hintText, hintStyle: const TextStyle( color: Colors.black54, ), contentPadding: const EdgeInsets.only(left: 20), ), // textAlign: TextAlign.center, // Align vertically and horizontally ); } } class ForgotPassword extends StatelessWidget{ @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: SafeArea( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 40), child: Column( children: <Widget>[ const SizedBox(height: 10), const SampleTextField(hintText: 'Enter Your Email'), FlatButton( onPressed: () { }, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20), ), padding: const EdgeInsets.symmetric(vertical: 20), ), ] ), ), ), ), ); } }
I googled my problem but can't seem to find the solution to my problem. I do know that there is something wrong with my initialization but i cant seem to connect the dots and find out where am i going wrong