我想在DART构建的Chrome打包应用中使用通知。
我的pubspec.yaml是:
dependencies:
browser: any
chrome: any
transformers:
- chrome
我的main.dart是:
import 'dart:html';
import 'package:chrome/chrome_app.dart' as chrome;
void main() {
....
NotificationOptions notOptions = {
'message': 'this is a notification message'
};
chrome.notifications.create('id1', notOptions).then((id) => print('notification created'));
....
}
它给了我一个问题:“未定义的类'NotificationOptions'”
如果我在导入包的行中删除了“as chrome”,那么我在chrome.notifications.create(...)处报错,直到未定义“chrome”。
我在这里犯了什么错误,以及为chrome通知定义选项的正确方法是什么!
最佳答案
如果在导入中使用as chrome
,则必须在该包中以chrome
前缀引用标识符。
这是无效的Dart语法
NotificationOptions notOptions = {
'message': 'this is a notification message'
};
也许应该是这样的
import 'dart:html';
import 'package:chrome/chrome_app.dart' as chrome;
void main() {
.....
chrome.NotificationOptions notOptions = new chrome.NotificationOptions(type: chrome.TemplateType.BASIC,
iconUrl:'/icon.png',title:'notification title', message: 'this is a notification message');
chrome.notifications.create('id1', notOptions).then((id) => print('notification created'));
....
}
manifiedt.json也应该包含许可,例如:
"permissions": ["notifications"],
关于dart - DART lang上的通知-Chrome打包的应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596250/