iam still really new to Flutter. Iam using Riverpod together with Firebase and getting some crash after trying to logout. If someone could help it would be awesome! If its just a noob mistake iam sorry already:D
My main.dart looks like this:
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(ProviderScope(child: App()));
}
class App extends StatelessWidget {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
return FutureBuilder(
// Initialize FlutterFire
future: _initialization,
builder: (context, snapshot) {
// Check for Errors
if (snapshot.hasError) {
return Center(child: Text("Error"));
}
// Once complete, show application
if (snapshot.connectionState == ConnectionState.done) {
return MaterialApp(
home: MyApp(),
darkTheme: ThemeData.dark(),
);
}
// Otherwise, show loadin while waiting for init to complete
return LoadingScreen();
},
);
}
}
Then i have this short app.dart file.
class MyApp extends ConsumerWidget {
@override
Widget build(BuildContext context, ScopedReader watch) {
final userAsyncValue = watch(authStateChangesProvider);
return userAsyncValue.when(
data: (user) {
if (user == null) {
return LoginScreen();
} else {
return HomeScreen();
}
},
loading: () => CircularProgressIndicator(),
error: (_, __) => Container());
}
}
I have a providers file
final firebaseAuthProvider = Provider<FirebaseAuth>((ref) => FirebaseAuth.instance);
final authStateChangesProvider = StreamProvider<User?>((ref) => ref.watch(firebaseAuthProvider).authStateChanges());
and an AuthService file:
class AuthenticationService {
final _auth = FirebaseAuth.instance;
Future logInWithEmailAndPassword(email, password) async {
try {
await _auth.signInWithEmailAndPassword(email: email, password: password);
} on FirebaseAuthException catch (e) {
print(e.message);
}
}
Future logOut() async {
await _auth.signOut();
}
}
finally my Login Page looks like this just
class LoginScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final _formKey = GlobalKey<FormState>();
final _inputService = InputService();
final _emailcontroller = TextEditingController();
final _passwordcontroller = TextEditingController();
final _auth = AuthenticationService();
return Form(
key: _formKey,
child: Scaffold(
backgroundColor: Colors.black,
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextFormField(
decoration: InputDecoration(hintText: "Email", hintStyle: TextStyle(color: Colors.white)),
controller: _emailcontroller,
style: TextStyle(color: Colors.white),
keyboardType: TextInputType.emailAddress,
validator: (value) => _inputService.isInputValid(value, "email")),
TextFormField(
decoration: InputDecoration(hintText: "Password", hintStyle: TextStyle(color: Colors.white)),
obscureText: true,
controller: _passwordcontroller,
style: TextStyle(color: Colors.white),
validator: (value) => _inputService.isInputValid(value, "password"),
),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_auth.logInWithEmailAndPassword(_emailcontroller.text, _passwordcontroller.text);
}
},
child: Text(
"Login",
style: TextStyle(
fontSize: 20,
),
)),
],
),
),
),
);
}
}
and a real simple home screen
class HomeScreen extends StatelessWidget {
final _auth = AuthenticationService();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
_auth.logOut();
},
child: Text(
"Logout",
style: TextStyle(color: Colors.white),
))
],
));
}
}
Logging in works like a charm but as soon as i logout the app crashes and i just got a exited (sigterm) if you tell me how i can find more debug information in vscode i happily supply it:D
Also it actually seems to correctly log me out of Firebase, at least when reopening the app on the simulator iam back in the login screen. So i guess its something with the auth state change and rebuild of the ConsumerWidget in the MyApp class?
Would be awesome if someone could help! And if you think my code sucks in general be happy to correct me iam just trying to put together what i find in tutorials and always are happy to be corrected:D
Edit:
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
firebase_core: "^1.0.1"
firebase_auth: "^1.0.1"
flutter_riverpod: "^0.13.1+1"
obscureText
variable in theTextFormField
. If I removed the line in my code the app stopped crashing. I filed a bug: Flutter GitHub. For now, I switched to the stable channel where everything works as expected!