这是一个监听设备通知的类,我第一次在同一个main.dart页面上的单个项目中尝试它,它工作得很好,但是当我将该类复制到另一个项目中使用它并下载它的所有内容时,它向我显示了一个错误,我不明白为什么。
下面是课程:
import 'package:flutter_notification_listener/flutter_notification_listener.dart';
import 'dart:isolate';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'dart:async';
class NotificationsLog extends StatefulWidget {
@override
_NotificationsLogState createState() => _NotificationsLogState();
}
class _NotificationsLogState extends State<NotificationsLog> {
List<NotificationEvent> _log = [];
bool started = false;
bool _loading = false;
ReceivePort port = ReceivePort();
@override
void initState() {
initPlatformState();
super.initState();
}
// we must use static method, to handle in background
static void _callback(NotificationEvent evt) {
// HANDLING BACKGROUND NOTIFICATIONS :
print('GETTING INFO ');
print(evt.packageName); // PACKAGE USE TO SEND MESSAGE :
print(evt.text); // MESSAGE CONTENT :
print(evt.title); //SENDER NUMBER: OR HEADER
final SendPort? send = IsolateNameServer.lookupPortByName("_listener_");
if (send == null) print("can't find the sender");
send?.send(evt);
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
NotificationsListener.initialize(callbackHandle: _callback);
// this can fix restart<debug> can't handle error
IsolateNameServer.removePortNameMapping("_listener_");
IsolateNameServer.registerPortWithName(port.sendPort, "_listener_");
port.listen((message) => onData(message));
// don't use the default receivePort
// NotificationsListener.receivePort.listen((evt) => onData(evt));
bool? isR = await NotificationsListener.isRunning;
print("""Service is ${isR == false ? "not " : ""}aleary running""");
setState(() {
started = isR!;
});
}
void onData(NotificationEvent event) {
setState(() {
_log.add(event);
});
if (!event.packageName!.contains("example")) {
// TODO: fix bug
// NotificationsListener.promoteToForeground("");
}
print('GETTING INFO FRONT APP ');
print(event.packageName); // PACKAGE USE TO SEND MESSAGE :
print(event.text); // MESSAGE CONTENT :
print(event.title); //SENDER NUMBER: OR HEADER
}
void startListening() async {
print("start listening");
setState(() {
_loading = true;
});
bool? hasPermission = await NotificationsListener.hasPermission;
if (hasPermission == false) {
print("no permission, so open settings");
NotificationsListener.openPermissionSettings();
return;
}
bool? isR = await NotificationsListener.isRunning;
if (isR == false) {
await NotificationsListener.startService(
title: "we still with you ", description: "payai from feelsafe");
}
setState(() {
started = true;
_loading = false;
});
}
void stopListening() async {
print("stop listening");
setState(() {
_loading = true;
});
await NotificationsListener.stopService();
setState(() {
started = false;
_loading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Notifications Listener Example'),
),
body: Center(
child: ListView.builder(
itemCount: _log.length,
reverse: true,
itemBuilder: (BuildContext context, int idx) {
final entry = _log[idx]; // _log[idx]
return ListTile(
trailing:
Text(entry.packageName.toString().split('.').last),
title: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(entry.title ?? "<<no title>>"),
Text(entry.createAt.toString().substring(0, 19)),
Text(entry.text ?? "<<no content>>"),
],
),
));
})),
floatingActionButton: FloatingActionButton(
onPressed: started ? stopListening : startListening,
tooltip: 'Start/Stop sensing',
child: _loading
? Icon(Icons.close)
: (started ? Icon(Icons.stop) : Icon(Icons.play_arrow)),
),
);
}
}
我觉得这个错误
e: C:\Users\x\flutter\.pub-cache\hosted\pub.dartlang.org\flutter_notification_listener-1.3.2\android\src\main\kotlin\im\zoe\labs\flutter_notification_listener\NotificationEvent.kt: (135, 71): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Drawable?
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':flutter_notification_listener:compileDebugKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1m 3s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)
我搜索了错误,并尝试解决方案..如改变Kotlin版本和更新项目,然后清理它,但不幸的是,它没有工作。我真的不明白他的问题是什么
这是我使用的软件包https://pub.dev/packages/flutter_notification_listener
1条答案
按热度按时间nnsrf1az1#
AndroidManifest.xml:
安卓系统/bulid.gradle:
安卓系统/app/bulid.gradle: