0% found this document useful (0 votes)
3 views3 pages

Flutter Firebase Auth Code

The document provides Flutter code for implementing Firebase Authentication, including both signup and login functionalities. It consists of two main files: 'main.dart' for initializing Firebase and 'auth_screen.dart' for handling user authentication through email and password. The code includes UI elements for user input and feedback messages for successful or failed authentication attempts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Flutter Firebase Auth Code

The document provides Flutter code for implementing Firebase Authentication, including both signup and login functionalities. It consists of two main files: 'main.dart' for initializing Firebase and 'auth_screen.dart' for handling user authentication through email and password. The code includes UI elements for user input and feedback messages for successful or failed authentication attempts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Flutter Firebase Auth (Signup + Login) Code

[Link]

import 'package:flutter/[Link]';
import 'package:firebase_core/firebase_core.dart';

void main() async {


[Link]();
await [Link]();
runApp(MyApp());
}

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
home: AuthScreen(),
);
}
}
Flutter Firebase Auth (Signup + Login) Code

auth_screen.dart

import 'package:flutter/[Link]';
import 'package:firebase_auth/firebase_auth.dart';

class AuthScreen extends StatefulWidget {


@override
State<AuthScreen> createState() => _AuthScreenState();
}

class _AuthScreenState extends State<AuthScreen> {


final emailController = TextEditingController();
final passwordController = TextEditingController();
bool isLogin = true;

void toggleForm() {
setState(() {
isLogin = !isLogin;
});
}

Future<void> handleAuth() async {


final email = [Link]();
final password = [Link]();

try {
if (isLogin) {
await [Link](
email: email,
password: password,
);
[Link](context).showSnackBar(
SnackBar(content: Text('[Success] Login Successful')),
);
} else {
await [Link](
email: email,
password: password,
);
[Link](context).showSnackBar(
SnackBar(content: Text('[Success] Signup Successful')),
);
}
} on FirebaseAuthException catch (e) {
[Link](context).showSnackBar(
SnackBar(content: Text('[Error] ${[Link]}')),
);
}
}
Flutter Firebase Auth (Signup + Login) Code

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(isLogin ? "Login" : "Signup")),
body: Padding(
padding: [Link](16),
child: Column(
children: [
TextField(
controller: emailController,
decoration: InputDecoration(labelText: 'Email'),
),
TextField(
controller: passwordController,
obscureText: true,
decoration: InputDecoration(labelText: 'Password'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: handleAuth,
child: Text(isLogin ? "Login" : "Signup"),
),
TextButton(
onPressed: toggleForm,
child: Text(isLogin
? "Don't have an account? Signup"
: "Already have an account? Login"),
),
],
),
),
);
}
}

You might also like