ear 0.0.7 ear: ^0.0.7 copied to clipboard
A flutter plugin project to handle all coming notification for android platform platforms.
import 'package:ear/models/models.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:ear/ear.dart';
void main() => runApp(HomePage());
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late StreamSubscription<EarModel> _earSubscription;
final _messengerKey = GlobalKey<ScaffoldMessengerState>();
@override
void initState() {
super.initState();
initPlatformState();
}
/// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async => startListening();
void onData(EarModel event) {
try {
_messengerKey.currentState!
.showSnackBar(SnackBar(content: Text(event.message)));
} catch (error) {
print(error);
}
}
/// To get the infor of notification, you just subscribe to
/// [ Stream<EarModel>]
void startListening() {
try {
_earSubscription = Ear.instance.earStream!.listen(onData);
} on EarModel catch (exception) {
print(exception);
}
}
/// IMPORTANT: before you is killed, you must call cancel stream to
/// avoid memory leak
void stopListening() => _earSubscription.cancel();
@override
Widget build(BuildContext context) {
return MaterialApp(
scaffoldMessengerKey: _messengerKey,
home: Scaffold(
appBar: AppBar(title: const Text('Ear example app')),
floatingActionButton: FloatingActionButton(
onPressed: () async {
bool result = await Ear.register();
print(result
? "Start listenning successfully"
: "Start listenning failure");
},
),
),
);
}
@override
void dispose() {
stopListening();
super.dispose();
}
}