stream_notifications 1.0.0 stream_notifications: ^1.0.0 copied to clipboard
A new Flutter plugin.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:stream_notifications/stream_notifications.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
StreamNotifications _notifications;
StreamSubscription<LiveNotificationEvent> _subscription;
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
startListening();
}
void onData(LiveNotificationEvent event) {
print(event.toString());
}
void startListening() {
_notifications = new StreamNotifications();
try {
_subscription = _notifications.notificationStream.listen(onData);
} on NotificationException catch (exception) {
print(exception);
}
}
void stopListening() {
_subscription.cancel();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
),
);
}
}