Created
June 27, 2023 10:50
-
-
Save KevinSia/5ddcb368947183105fb899931b4c59e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:app/common_widgets/placeholders/loading_placeholder.dart'; | |
import 'package:app/common_widgets/wrappers/system_notifs_permissions_wrapper.dart'; | |
import 'package:app/feature/recent_notifications/application/recent_notifications_model.dart'; | |
import 'package:app/feature/recent_notifications/presentation/recent_notifs_page/recent_notifs_scaffold.dart'; | |
import 'package:app/feature/recent_notifications/presentation/recent_notifs_page/recent_notifs.dart'; | |
import 'package:app/graphql/codegen/graphql_api.dart'; | |
import 'package:app/utils/graphql_provider.dart'; | |
import 'package:app/utils/sentry_exception.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
import 'package:graphql_flutter/graphql_flutter.dart'; | |
// Import the firebase_core and cloud_firestore plugin | |
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'package:app/router/router.dart'; | |
class RecentNotifsLoader extends StatefulWidget { | |
const RecentNotifsLoader({super.key}); | |
@override | |
RecentNotifsLoaderState createState() => RecentNotifsLoaderState(); | |
} | |
class RecentNotifsLoaderState extends State<RecentNotifsLoader> { | |
List<String> messages = []; | |
String collectionPath = 'chats/dAa2bRZVCdha5b44ElSd/messages'; | |
bool hasInit = false; | |
@override | |
void initState() { | |
super.initState(); | |
CollectionReference reference = | |
FirebaseFirestore.instance.collection(collectionPath); | |
reference.snapshots().listen((querySnapshot) { | |
if (!hasInit) { | |
setState(() { | |
hasInit = true; | |
}); | |
return; | |
} | |
querySnapshot.docChanges.forEach((change) { | |
if (change.type.name == 'added') { | |
setState(() { | |
final data = change.doc.data() as Map<String, dynamic>; | |
final message = "${data['username']}: ${data['content']}"; | |
messages.add(message); | |
}); | |
} | |
// Do something with change | |
}); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: ListView.builder( | |
itemCount: messages.length, | |
itemBuilder: (context, index) => Text(messages[index]), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment