I am making a chatting app by following a Udemy tutorial. I can’t seem to find the solution to this error that has been occuring and other solutions on stackoverflow either don’t seem to work or I don’t know how to implement them in my own code. The problem lies in my registration screen, the code is attached below
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import '../components.dart';
import 'chat_screen.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
class RegistrationScreen extends StatefulWidget {
static String id = 'registration_screen';
@override
_RegistrationScreenState createState() => _RegistrationScreenState();
}
class _RegistrationScreenState extends State<RegistrationScreen> {
final _auth = FirebaseAuth.instance;
late String email;
late String password;
@override
void initState(){
super.initState();
Firebase.initializeApp();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Hero(
tag: 'logo',
child: SizedBox(
height: 200.0,
child: Image.asset('images/logo.png'),
),
),
const SizedBox(
height: 48.0,
),
CustomTextField(text: 'Enter your email', onChanged: (value){email = value;},obscure: false,),
const SizedBox(
height: 8.0,
),
CustomTextField(text: 'Enter your password', onChanged: (value){password=value;},obscure: true,),
const SizedBox(
height: 24.0,
),
CustomButton(color: Colors.blueAccent, title: 'Register', onPressed: ()async{
try{
final newUser = await _auth.createUserWithEmailAndPassword(email: email, password: password);
Navigator.pushNamed(context, ChatScreen.id);
print(newUser);
}
catch(e){
print(e);
}
}),
],
),
),
);
}
}
I asked chatGPT to solve this problem and it came up with a code that also didn’t work:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import '../components.dart';
import 'chat_screen.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
class RegistrationScreen extends StatefulWidget {
static String id = 'registration_screen';
@override
_RegistrationScreenState createState() => _RegistrationScreenState();
}
class _RegistrationScreenState extends State<RegistrationScreen> {
final _auth = FirebaseAuth.instance;
late String email;
late String password;
// Future to handle Firebase initialization
late Future<FirebaseApp> _initialization;
@override
void initState() {
super.initState();
_initialization = Firebase.initializeApp();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
return Scaffold(
body: Center(
child: Text("Error: ${snapshot.error}"),
),
);
}
// Once complete, show the application
if (snapshot.connectionState == ConnectionState.done) {
return Scaffold(
backgroundColor: Colors.white,
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Hero(
tag: 'logo',
child: SizedBox(
height: 200.0,
child: Image.asset('images/logo.png'),
),
),
const SizedBox(
height: 48.0,
),
CustomTextField(
text: 'Enter your email',
onChanged: (value) {
email = value;
},
obscure: false,
),
const SizedBox(
height: 8.0,
),
CustomTextField(
text: 'Enter your password',
onChanged: (value) {
password = value;
},
obscure: true,
),
const SizedBox(
height: 24.0,
),
CustomButton(
color: Colors.blueAccent,
title: 'Register',
onPressed: () async {
try {
final newUser = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
if (newUser != null) {
Navigator.pushNamed(context, ChatScreen.id);
}
} catch (e) {
print(e);
}
},
),
],
),
),
);
}
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
},
);
}
}
I followed the code from a tutorial I was following, which perhaps might be outdated. I was expecting for the screen to at least load, but as soon as the user goes to the registration screen the app crashes and shows the error mentioned above.
Aneesh Talwalkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1