未定义名称“firebase”颤振
Posted
技术标签:
【中文标题】未定义名称“firebase”颤振【英文标题】:Undefined name 'firebase' Flutter 【发布时间】:2021-05-01 09:39:47 【问题描述】:您好,我正在尝试在我的 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),
),
]
),
),
),
),
);
我用谷歌搜索了我的问题,但似乎找不到解决问题的方法。我确实知道我的初始化有问题,但我似乎无法连接点并找出我哪里出错了
【问题讨论】:
你在哪里得到这个错误? @fartem 当我初始化这个最终的 FirebaseApp _fbApp=Firebase.initializeApp(); @RishailSiddiqui 请检查我的答案 在 Firebase 初始化后尝试初始化一个变量。 【参考方案1】:我通过升级到更新版本的firebase_core
解决了这个问题(我选择了1.0.0
)
【讨论】:
【参考方案2】:您必须添加 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】:你需要将你的 Firebase 初始化到你的 main 方法中
第一个flutter pub get
Future<void> main() async
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(HomePage());
【讨论】:
你试过用flutter clean然后flutter pub get 这没有任何效果【参考方案4】:正如 Shubham 已经提到的,您必须在使用 Firebase 实例之前对其进行初始化 - 尽管不一定必须将其放入 main.xml 中。 您可以在尝试使用 Firebase 实例的小部件之外的任何位置初始化它。 Firebase 懒惰地创建它的实例(很可能通过代理模式),因为它的创建成本很高。 因此,在您尝试引用它后它无法立即找到它:)
【讨论】:
我尝试初始化它,然后在我使用 firebase 实例的位置引用它,就像我们在其他 OOP 语言中所做的那样,但它仍然给我一个错误【参考方案5】:将“firebase_core:^1.7.0”添加到 pubspec.yaml 文件。然后flutter pub得到。问题会解决的。
【讨论】:
以上是关于未定义名称“firebase”颤振的主要内容,如果未能解决你的问题,请参考以下文章
颤振未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义名称
颤振中permission_handler包的未定义名称“权限”