Firebase / initializeApp()
Closed this issue · 3 comments
Hello,
First of all, thank you for the code, it can have a lot of value.
That being said, I found some issues when trying to start the app.
First I had to go to pubspec.yaml
and do:
assets:
- assets/lang/en.json
- assets/lang/zh.json
fonts:
- family: ProductSans
fonts:
- asset: assets/fonts/Product-Sans-Regular.ttf
- asset: assets/fonts/Product-Sans-Italic.ttf
style: italic
- asset: assets/fonts/Product-Sans-Bold.ttf
weight: 700
- asset: assets/fonts/Product-Sans-Bold-Italic.ttf
style: italic
weight: 700
and change line 27 on the app_localizations.dart
to
await rootBundle.loadString('assets/lang/${locale.languageCode}.json');
or the app wouldn't even start.
Then I had to add
await Firebase.initializeApp();
between lines 15/16 of the main.dart and main_prod.dart, or again, the app wouldn't even start. (Also had to add firebase_core: ^1.6.0
to the pubspec.yaml
, and the import to main.dart
and and main_prod.dart
of course).
Then in line 81 of the my_app.dart
I had to change it to:
return userSnapshot.data?.uid != 'null'
? HomeScreen()
: SignInScreen();
because userSnapshot.hasData
always had "something", and that was causing records to be inserted in the database with uid as null
meaning /user/null/todos/<todo_item_id>
.
But now I can't move on.
The issue itself is when I log out and login with another user, I have the data from the old user.
I know you say on the README.md
that:
To achieve this, FirestoreDatabase will be re-created everytime onAuthStateChanged changed.
But this doesn't seem to be happening.
Might be due to my changes as mentioned above.
Any help would be much appreciated. Regards to all.
Seems I have "fixed" the problem, in what I admit is a non-ideal way.
Here's the workaround
Create a lib/restart_app.dart
import 'package:flutter/material.dart';
class RestartWidget extends StatefulWidget {
RestartWidget({required this.child});
final Widget child;
static void restartApp(BuildContext context) {
context.findAncestorStateOfType<_RestartWidgetState>()!.restartApp();
}
@override
_RestartWidgetState createState() => _RestartWidgetState();
}
class _RestartWidgetState extends State<RestartWidget> {
Key key = UniqueKey();
void restartApp() {
setState(() {
key = UniqueKey();
});
}
@override
Widget build(BuildContext context) {
return KeyedSubtree(
key: key,
child: widget.child,
);
}
}
Import it in lib/main.dart
and lib/main_prod.dart
and surround the MyApp
widget with it.
RestartWidget(
child: MyApp(
databaseBuilder: (_, uid) => FirestoreDatabase(uid: uid),
key: Key('wdev'),
),
),
Import lib/restart_app.dart
also in lib/ui/setting/setting_screen.dart
and replace these three lines
Navigator.pop(context);
Navigator.of(context).pushNamedAndRemoveUntil(
Routes.login, ModalRoute.withName(Routes.login));
with
RestartWidget.restartApp(context);
What this does, is re-start the app which in itself restarts the FirestoreDatabase
which uses the uid
gotten only once on app start.
This way, on logout, the database is reset to the initial state (no user logged in).
Again, this isn't great, but it works.
@arturjnt hello there. Thanks a lot for sharing this and also the details info. The RestartWidget seems to be a quick fix.
Very sorry for late response, was working on another projects. I'm open up to anyone that are interested to continue improving this open project.
I also have been encountering the same issues. I had a good chuckle to myself when I saw that someone had the exact same issues and we even implemented the same kind of fixes even though I hadn't taken a look at this post.