Skip to content

Commit

Permalink
added provider to listen firebase changes
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinPopovskikh committed Sep 22, 2021
1 parent b69aa14 commit 20ac288
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ This app is used to test the following features:
- ```Clean DB``` - removes all tables from SQLite database and creates them again;
- ```Old Json Caching Example``` - fetching data from REST API and caching into SQLite;

- Firebase notifications are also enabled. FCM token is shown in debug console at startup. Copy the token and go to "Cloud messaging" in Firebase console to test in out.
- Firebase notifications are also enabled. FCM token is shown in debug console at startup. Copy the token and go to "Cloud messaging" in Firebase console to test it out.

25 changes: 25 additions & 0 deletions lib/backend/firestore_changes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';

class FirestoreChanges extends ChangeNotifier {
String changeStr = '';

void listenOnFirestoreChanges() {
FirebaseFirestore.instance
.collection('flutter-caching')
.snapshots()
.listen((event) {
changeStr = '';
for (var change in event.docChanges) {
// print('Change type: ${change.type}; Data: '
// '${change.doc.data()}\n');
changeStr = changeStr +
'Change type: ${change.type};\n'
'Doc ID: ${change.doc.id}\n'
'Data: ${change.doc.data()}\n';
}
notifyListeners();
// changeStr = '';
});
}
}
15 changes: 11 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fetchingapp/backend/firestore_changes.dart';
import 'package:fetchingapp/backend/notifications.dart';
import 'package:fetchingapp/screens/home.dart';
import 'package:fetchingapp/screens/login.dart';
Expand All @@ -13,6 +14,7 @@ Future<void> main() async {
await Firebase.initializeApp();

initializeMessaging();
// listenOnFirestoreChanges();

var token = await FirebaseMessaging.instance.getToken();
print('FCM token: $token');
Expand Down Expand Up @@ -49,11 +51,16 @@ class _MyAppState extends State<MyApp> {
firstScreen = const LoginPage();
}

return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,


return ChangeNotifierProvider(
create: (_) => FirestoreChanges(),
child: MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: firstScreen,
),
home: firstScreen,
);
}
}
7 changes: 6 additions & 1 deletion lib/screens/home.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:fetchingapp/backend/database.dart';
import 'package:fetchingapp/backend/firestore_changes.dart';
import 'package:fetchingapp/backend/google_authentication.dart';
import 'package:fetchingapp/screens/firestore_stream.dart';
import 'package:fetchingapp/screens/firestore_future.dart';
Expand All @@ -7,6 +8,7 @@ import 'package:fetchingapp/screens/login.dart';
import 'package:fetchingapp/screens/provider_test.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class HomePage extends StatelessWidget {
const HomePage(this.user, {Key? key}) : super(key: key);
Expand All @@ -15,6 +17,9 @@ class HomePage extends StatelessWidget {

@override
Widget build(BuildContext context) {
Provider.of<FirestoreChanges>(context, listen: false)
.listenOnFirestoreChanges();

return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
Expand All @@ -27,7 +32,7 @@ class HomePage extends StatelessWidget {
const SizedBox(height: 10),
testSectionButton(
context: context,
name: 'Provider Test',
name: 'Simple Provider Test',
widget: const ProviderTestPage()),
const SizedBox(height: 10),
testSectionButton(
Expand Down
13 changes: 12 additions & 1 deletion lib/screens/provider_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fetchingapp/backend/firestore_changes.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

Expand All @@ -22,8 +23,18 @@ class _ProviderTestPageState extends State<ProviderTestPage> {
const Button(fruit: 'Orange'),
const Button(fruit: 'Banana'),
Text(
'My favorite fruit is: ' + Provider.of<Favorites>(context).fruit,
'My favorite fruit is: ' +
Provider.of<Favorites>(context).fruit +
'\n\n',
style: const TextStyle(fontSize: 25),
),
const Text(
'Firestore Listener Provider:',
style: TextStyle(fontSize: 25),
),
Text(
'\n' + Provider.of<FirestoreChanges>(context).changeStr,
style: const TextStyle(fontSize: 15),
)
],
),
Expand Down

0 comments on commit 20ac288

Please sign in to comment.