import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:loris/pages/notifications/single_notif.dart'; import '../../business_logic/websocket.dart' as websocket; final notifStream = StreamController(); class Notifications extends StatefulWidget { const Notifications({Key? key}) : super(key: key); @override State createState() => _NotificationsState(); } class _NotificationsState extends State { List notifs = []; @override void initState() { for (int i = 0; i < websocket.map.length; i++) { final keyI = websocket.map.keys.toList()[i]; websocket.map[keyI]!["home"]!.stream.listen((event) async { Map json = jsonDecode(event); if (json["event"] == "notification") { SingleNotif notif = SingleNotif( content: jsonDecode(json["payload"]), ); if (mounted) { setState(() { notifStream.sink.add(1); notifs.insert(0, notif); }); } } }); } super.initState(); } @override Widget build(BuildContext context) { return ListView.separated( itemBuilder: ((context, index) => notifs[index]), separatorBuilder: (context, index) => const SizedBox( height: 8, ), itemCount: notifs.length); } }