匿名函数和回调...为什么不起作用?



>我需要将参数传递给 Flutter 中的回调。

AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 5)), int.parse(id), alarmCallback); //WORKS

回调为:

void alarmCallback(String id){
print("Hello, " + id);
}

我使用匿名函数方式传递参数,但它不起作用,回调没有执行:

AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 5)), int.parse(id), () => alarmCallback(id)); //DOESN'T WORK

我以这种方式传递参数,但它不起作用,回调没有执行,当我将光标放在 id 上时,IDE 向我显示它是动态的,但它是字符串:

AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 5)), int.parse(id), (id) => alarmCallback(id)); //DOESN'T WORK

提前感谢!

我写了一个简短的例子。此代码记录定期警报 + 一次性警报。回调函数将int作为参数。

请记住,自 API 19 (Build.VERSION_CODES.KITKAT(警报传递不准确

请参阅
https://stackoverflow.com/a/56012077/2632894 https://developer.android.com/reference/android/app/AlarmManager

我正在运行以下版本的Flutter和Dart:

Flutter 1.13.7-pre.31 • channel master • https://github.com/flutter/flutter.git
Framework • revision ee7a37f1d3 (4 hours ago) • 2020-01-07 00:00:00 -0000
Engine • revision 3851981b86
Tools • Dart 2.8.0 (build 2.8.0-dev.0.0 2f57602411)
android_alarm_manager: ^0.4.5+2

主飞镖

import 'package:flutter/material.dart';
import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'dart:isolate';
void callbackFunction(int id) async {
final DateTime now = DateTime.now();
final int isolateId = Isolate.current.hashCode;
print('[$now] Param=$id isolate=$isolateId function=$callbackFunction');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
AndroidAlarmManager.initialize();
runApp(MyApp());
final int alarmID = 42;
await AndroidAlarmManager.periodic(const Duration(minutes: 1), alarmID,
callbackFunction, wakeup: true, exact: true, rescheduleOnReboot:true).then((val) => print('periodic alarm set up:' + val.toString()));
final int anotherAlarmID = 666;
AndroidAlarmManager.oneShot(
Duration(seconds: 3),
anotherAlarmID,
callbackFunction,
wakeup: true,
).then((val) => print('oneShot alarm set up:' + val.toString()));
}
...
...

安卓清单.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.full.flutter_app_new">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:name="io.flutter.app.FlutterApplication"
android:label="flutter_app_new"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name="io.flutter.plugins.androidalarmmanager.AlarmService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
<receiver
android:name="io.flutter.plugins.androidalarmmanager.AlarmBroadcastReceiver"
android:exported="false"/>
<receiver
android:name="io.flutter.plugins.androidalarmmanager.RebootBroadcastReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>

Ouptut 应该类似于这个:

I/FlutterBackgroundExecutor(12111): Starting AlarmService...
I/AndroidAlarmManagerPlugin(12111): onAttachedToEngine
I/flutter (12111): periodic alarm set up:true
I/flutter (12111): oneShot alarm set up:true
I/AlarmService(12111): AlarmService started!
I/flutter (12111): [2020-01-07 00:05:33.065838] Param=666! isolate=470178969 function='Closure: (int) => void from Function 'callbackFunction': static.'
I/flutter (12111): [2020-01-07 00:06:26.158709] Param=42! isolate=470178969 function='Closure: (int) => void from Function 'callbackFunction': static.'

胖箭头语法如下所示:

(params) => function(params)

与 相同:

type some_name(params){
return  function(params)
}

来自 oneShotAt 方法的文档:

回调可以是 Function(( 或 Function(int(

id 将传递给回调,如果它是函数(int(的类型

所以它可以带或不带 id,并且这个 id 必须是int。 由于你不太担心函数的返回,你可以直接传递一个函数:

(int id){print("Hello, " + id);}

这不会返回任何东西。

在您的通话中,它应该是这样的:

AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 5)), int.parse(id), (int id){alarmCallback(id);});

我不知道你得到了什么日志错误,但试试这个:AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 5)), int.parse(id), (id) => alarmCallback(id));

相关内容

  • 没有找到相关文章

最新更新