您好,我正在尝试在我的 Flutter 应用程序中实施 Firebase 身份验证,但出现此错误 未定义的名称“firebase”
以下是代码供参考:
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),
),
]
),
),
),
),
);
}
}
我用谷歌搜索了我的问题,但似乎无法找到我的问题的解决方案。我确实知道我的初始化有问题,但我似乎无法将这些点联系起来并找出我哪里出错了
答案 0 :(得分:1)
您需要将 Firebase 初始化为您的主要方法
第一个flutter pub get
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(HomePage());
}
答案 1 :(得分:1)
我通过升级到更新版本的 firebase_core
解决了这个问题(我选择了 1.0.0
)
答案 2 :(得分:1)
您必须添加 firebase 核心包
firebase_core: ^7.0.1(查看实际包版本) 扑扑酒吧得到
并将包添加到您的代码中
import 'package:firebase_core/firebase_core.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(HomePage());
}
答案 3 :(得分:0)
正如 Shubham 已经提到的,您必须在使用 Firebase 实例之前对其进行初始化 - 尽管它不一定必须放入 main.js 中。 您可以在您尝试使用 Firebase 实例的小部件之外的任何地方对其进行初始化。 Firebase 懒惰地创建它的实例(很可能通过代理模式),因为它的创建成本很高。 因此,在您尝试引用它后无法立即找到它:)