Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

android studio - Undefined name 'firebase' Flutter

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'),
              

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...