'Flutter not updating firebase collection
I can't seem to get my flutter app to update my firebase collection when I get a user to sign up. I get no error messages or any warnings, only thing I can think of is if something is null and therefore not updating but I can't see how since my authentication works and I can see the user in firebase authentication tab.
I've checked my rules and it looks okay:
My code is as follows:
///sign up function
Future<String?> signUp(
{required String email, required String password}) async {
try {
await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
var currentUser = FirebaseAuth.instance.currentUser;
FirebaseFirestore.instance.collection('USER_TABLE').doc().update({
'email': currentUser!.email,
'firstName': 'Joe',
'lastName': 'bloggs',
'userID': currentUser.uid,
});
return "Signed up";
} on FirebaseAuthException catch (e) {
return e.message;
}
my database looks as follows:
Main.dart code
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<AuthenticationService>(
create: (_) => AuthenticationService(FirebaseAuth.instance),
),
StreamProvider(
create: (context) =>
context.read<AuthenticationService>().authStateChanges,
initialData: null,
)
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: AuthenticationWrapper(),
),
);
}
}
class AuthenticationWrapper extends StatelessWidget {
const AuthenticationWrapper({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final firebaseUser = context.watch<User>();
if (firebaseUser == null) {
print('User is NOT signed in!');
return const WelcomeScreen();
}
print('User is signed in!');
return const MainScreen();
}
}
Solution 1:[1]
I see the issue "update method needs an ID" only "add method doesn’t "
FirebaseFirestore.instance.collection('USER_TABLE').doc(currentUser.uid).update({
'email': currentUser!.email,
'firstName': 'Joe',
'lastName': 'bloggs',
'userID': currentUser.uid,
});
or
FirebaseFirestore.instance.collection('USER_TABLE').add({
'email': currentUser!.email,
'firstName': 'Joe',
'lastName': 'bloggs',
'userID': currentUser.uid,
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


