I implemented the authentication by an anonymous account in Firebase Authentication with Flutter.
The signup/login successes, has no errors, and everything works.
However, the dashboard doesn't have a user as below.
May I know the reason?
My code
main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {...}
class MyHomePage extends StatefulWidget {...}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}
void dispose() {
super.dispose();
print("dispose");
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<TimerProvider>(
create: (context) => TimerProvider()..init(),
child: MainPage()
);
}
}
class MainPage extends StatefulWidget {...}
class _MainPageState extends State<MainPage> {...}
providers.dart
class TimerProvider with ChangeNotifier {
String? uid;
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future init() async {
try {
await signupUser();
} catch(err){
print('init error: ${err}');
}
}
Future signupUser() async {
_firebaseAuth.signInAnonymously();
uid = _firebaseAuth.currentUser?.uid;
notifyListeners();
}
...
}
