I added TouchID to my SignIn Page, When i run the code, The app is opening in simulator and I can see the finger print area that i created and when i touch it and try to log in via TouchID i can enter the finger print when it can sense but after that in console, it says
======== Exception caught by widgets library ======================================================= The following NoSuchMethodError was thrown building ProfileScreen(dirty, dependencies: [MediaQuery], state: _ProfileScreenState#7079c): The getter 'email' was called on null. Receiver: null Tried calling: email
The relevant error-causing widget was: ProfileScreen file:///Users/iremguner/AndroidStudioProjects/healtish_app/lib/authenticate/sign_in.dart:77:95 When the exception was thrown, this was the stack: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 AuthService.getCurrentUser (package:healtish_app/services/auth.dart:24:30) #2 _ProfileScreenState.build.getProfile (package:healtish_app/ui/pages/profile_screen.dart:73:43) #3 _ProfileScreenState.build (package:healtish_app/ui/pages/profile_screen.dart:243:37) #4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27) ...
and then app is crashing.
import 'package:healtish_app/services/auth.dart';
import 'package:healtish_app/shared/constants.dart';
import 'package:healtish_app/shared/loading.dart';
import 'package:flutter/material.dart';
import 'package:healtish_app/ui/pages/calorie_calculator_screen.dart';
import 'package:healtish_app/ui/pages/profile_screen.dart';
import 'package:flutter/services.dart';
import 'package:local_auth/local_auth.dart';
class SignIn extends StatefulWidget {
final Function toggleView;
SignIn({this.toggleView});
@override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
final LocalAuthentication localAuth = LocalAuthentication();
final AuthService _auth = AuthService();
final _formKey = GlobalKey<FormState>();
String error = '';
bool loading = false;
// text field state
String email = '';
String password = '';
@override
Widget build(BuildContext context) {
return loading
? Loading()
: Scaffold(
backgroundColor: Color(0xFFE9E9E9),
appBar: AppBar(
backgroundColor: Color(0xFFE0AD61),
elevation: 0.0,
title: Text(
'Sign in',
style: TextStyle(color: Color(0XFF200087), fontSize: 20),
),
actions: <Widget>[
FlatButton.icon(
icon: Icon(
Icons.person,
color: Color(0XFF200087),
),
label: Text('Register',
style: TextStyle(color: Color(0XFF200087), fontSize: 20)),
onPressed: () => widget.toggleView(),
),
],
),
body: Container(
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
child: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Hero(
tag: 'logo',
child: Container(
child: Image.asset(
'assets/logo.png',
)),
),
GestureDetector(
onTap: () async{
bool weCanCheckBiometrics = await localAuth.canCheckBiometrics;
if(weCanCheckBiometrics){
bool authenticated = await localAuth.authenticateWithBiometrics(
localizedReason: "Authenticate to see your bank statement.",
);
if(authenticated){
Navigator.push(context, MaterialPageRoute(builder: (context) =>
ProfileScreen()));
}
}
},
child: Row(
children: <Widget>[
Icon(
Icons.fingerprint,
size: 35.0,
),
SizedBox(
width: 10,
),
Text(
"Use Touch Id",
style: TextStyle(
color: Color(0XFF200087),
fontSize: 14,
fontWeight: FontWeight.bold),
),
],
),
),
Divider(color: const Color(0xFFE3B3D2)),
SizedBox(height: 20.0),
TextFormField(
decoration: textInputDecoration.copyWith(
hintText: 'Please Enter Email'),
validator: (val) =>
val.isEmpty ? 'Enter an email' : null,
onChanged: (val) {
setState(() => email = val);
},
),
SizedBox(height: 20.0),
TextFormField(
obscureText: true,
decoration: textInputDecoration.copyWith(
hintText: 'Please Enter Password'),
validator: (val) => val.length < 6
? 'Enter a password 6+ chars long'
: null,
onChanged: (val) {
setState(() => password = val);
},
),
SizedBox(height: 20.0),
RaisedButton(
color: const Color(0XFF200087),
child: Text(
' Sign In ',
style: TextStyle(color: Colors.white),
),
onPressed: () async {
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await _auth
.signInWithEmailAndPassword(email, password);
if (result == null) {
setState(() {
loading = false;
error =
'Could not sign in with those credentials';
});
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProfileScreen(
calorie: 1500.0,
)),
);
}
}
}),
SizedBox(height: 12.0),
Text(
error,
style: TextStyle(color: Colors.red, fontSize: 14.0),
),
],
),
),
),
),
);
}
}