Open Aravindpr11 opened 8 months ago
import 'package:cloud_functions/cloud_functions.dart'; import 'package:dialogico/app.dart'; import 'package:dialogico/screens/homescreen.dart'; import 'package:firebase_auth/firebase_auth.dart' as firebase; import 'package:flutter/material.dart'; import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart';
void main(){ runApp(MaterialApp(home: Signup(),)); } class Signup extends StatefulWidget { static Route get route => MaterialPageRoute( builder: (context) => Signup(), );
@override
State
class _SignupState extends State
final _formKey = GlobalKey
final emailRegex = RegExp( r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^`{|}~]+@[a-zA-Z0-9]+.[a-zA-Z]+");
bool _loading = false;
Future
// Debugging: Print user information
print('User: $user');
// Move the 'mounted' check here
if (mounted) {
if (user == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('User is empty')),
);
return;
}
}
// Set Firebase display name and profile picture
List<Future<void>> futures = [
creds.user!.updateDisplayName(_nameController.text),
if (_profilePictureController.text.isNotEmpty)
creds.user!.updatePhotoURL(_profilePictureController.text)
];
await Future.wait(futures);
// Create Stream user and get token using Firebase Functions
final results = await functions
.httpsCallable('ext-auth-chat-getStreamUserToken')
.call();
// Connect user to Stream and set user data
if (!mounted) return;
final client = StreamChatCore.of(context).client;
final streamUser = User(
id: creds.user!.uid,
name: _nameController.text,
image: _profilePictureController.text,
);
await client.connectUser(
streamUser,
results.data,
);
await client.updateUser(streamUser);
if (!mounted) return;
// Navigate to home screen
await Navigator.of(context).pushReplacement(HomeScreen.route);
} on firebase.FirebaseAuthException catch (e) {
print('Firebase Auth Error: ${e.message}');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e.message ?? 'Auth error')),
);
} catch (e, st) {
print('Error: $e');
print('Stack Trace: $st');
logger.e('Sign up error', error: e, stackTrace: st);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('An error occurred')),
);
}
setState(() {
_loading = false;
});
}
}
String? _nameInputValidator(String? value) { if (value == null || value.isEmpty) { return 'Cannot be empty'; } return null; }
String? _emailInputValidator(String? value) { if (value == null || value.isEmpty) { return 'Cannot be empty'; } if (!_emailRegex.hasMatch(value)) { return 'Not a valid email'; } return null; }
String? _passwordInputValidator(String? value) { if (value == null || value.isEmpty) { return 'Cannot be empty'; } if (value.length <= 6) { return 'Password needs to be longer than 6 characters'; } return null; }
@override void dispose() { _emailController.dispose(); _passwordController.dispose(); _profilePictureController.dispose(); _nameController.dispose(); super.dispose(); }
@override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color(0xfFE343333), // appBar: AppBar(title: Text("Sign Up",style: TextStyle(color: Colors.white), // ),backgroundColor: Color(0xfFE343333), ), body: (_loading) ? const Center(child: CircularProgressIndicator()) : SingleChildScrollView( child: Center( child: Column( children: [ SizedBox(height: 70,),
Image.asset("assets/images/message-EaIj2A4c02.png",height: 100,width: 100,fit: BoxFit.fill,),
SizedBox(height: 5,),
Center(child: Text("Sign UP Here",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 15),),),
SizedBox(height: 5,),
Padding(padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
child: TextFormField(
controller: _nameController,
validator: _nameInputValidator,
style: TextStyle(color: Colors.white),
textInputAction: TextInputAction.next,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30)
),
hintText: " Name",
hintStyle: TextStyle(color: Colors.white,)
),
keyboardType: TextInputType.name,
autofillHints: const [
AutofillHints.name,
AutofillHints.username
],
)),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: _profilePictureController,
style: TextStyle(color: Colors.white),
textInputAction: TextInputAction.next,
decoration:
InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30)
),
hintText: " Image url",
hintStyle: TextStyle(color: Colors.white,)
),
keyboardType: TextInputType.url,
)),
// Padding(padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
// child: TextFormField(
// style: TextStyle(color: Colors.white),
// textInputAction: TextInputAction.next,
// decoration: InputDecoration(
// border: OutlineInputBorder(
// borderRadius: BorderRadius.circular(30)
// ),
// hintText: "Last name",
// hintStyle: TextStyle(color: Colors.white,)
// ))),
Padding(padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
child: TextFormField(
style: TextStyle(color: Colors.white),
textInputAction: TextInputAction.next,
controller: _emailController,
validator: _emailInputValidator,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30)
),
hintText: "Email",
hintStyle: TextStyle(color: Colors.white,)
),
keyboardType: TextInputType.emailAddress,
autofillHints: const [AutofillHints.email],
),
),
SizedBox(height: 5,),
// Padding(padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
// child: TextFormField(
// style: TextStyle(color: Colors.white),
// textInputAction: TextInputAction.next,
// validator: (email){
// if(email!.isEmpty || email.contains("@")){
// return"Enter a valid eamil id";
// }
// else return null;
// },
// decoration: InputDecoration(
// border: OutlineInputBorder(
// borderRadius: BorderRadius.circular(30)
// ),
// hintText: "Email id ",
// hintStyle: TextStyle(color: Colors.white,)
// ),
// ),
// ),
Padding(padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
child: TextFormField(
controller: _passwordController,
validator: _passwordInputValidator,
obscuringCharacter: "*",
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30)
),
hintText: "Password",
hintStyle: TextStyle(color: Colors.white,),
),
obscureText: true,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.visiblePassword,
),
),
// Padding(padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
// child: TextFormField(
// obscureText: showpwd1,
// obscuringCharacter: "*",
// decoration: InputDecoration(
// border: OutlineInputBorder(
// borderRadius: BorderRadius.circular(30)
// ),
// hintText: "Conform Password",
// hintStyle: TextStyle(color: Colors.white,),
//
// suffixIcon: IconButton(onPressed: (){
// setState(() {
// if(showpwd1==true){
// showpwd1 = false;
// }
// else{
// showpwd1 = true;
// }
// });
// }, icon: Icon(showpwd1 == true?Icons.visibility:Icons.visibility_off,color: Colors.white,)),
// ),
// validator: (cpassword){
// if(cpassword != confirmpass || cpassword!.isEmpty){
// return"password mismatch / empty";
// }
// else return null;
// },
//
// ),
// ),
SizedBox(height: 10,),
ElevatedButton(
onPressed: _signUp,
child: const Text('Sign up'),
),
const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Divider(),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Already have an account?'),
// style: TextStyle(fontStyle: font),
SizedBox(width: 8),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Sign in'),
),
],
),
],
),
),
),
);
} } this is my signup code
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value E/flutter ( 6983): #0 _SignupState._signUp (package:dialogico/home/signup.dart:38:30) E/flutter ( 6983): #1 _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1183:21) E/flutter ( 6983): #2 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:275:24) E/flutter ( 6983): #3 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:652:11) E/flutter ( 6983): #4 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:309:5) E/flutter ( 6983): #5 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:279:7) E/flutter ( 6983): #6 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:167:27) E/flutter ( 6983): #7 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:492:20) E/flutter ( 6983): #8 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:468:22) E/flutter ( 6983): #9 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:439:11) E/flutter ( 6983): #10 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:413:7) E/flutter ( 6983): #11 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:376:5) E/flutter ( 6983): #12 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:323:7) E/flutter ( 6983): #13 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:292:9) E/flutter ( 6983): #14 _invoke1 (dart:ui/hooks.dart:328:13) E/flutter ( 6983): #15 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:410:7) E/flutter ( 6983): #16 _dispatchPointerDataPacket (dart:ui/hooks.dart:262:31) E/flutter ( 6983):
This is happening when i click signup