ear 0.0.6 copy "ear: ^0.0.6" to clipboard
ear: ^0.0.6 copied to clipboard

PlatformAndroidiOS
outdated

A flutter plugin project to handle all coming notification for android platform platforms.

example/lib/main.dart

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;

  @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) => print(event.packageName);

  /// 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(
      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();
  }
}
42
likes
150
pub points
0%
popularity

Publisher

unverified uploader

A flutter plugin project to handle all coming notification for android platform platforms.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on ear