Dart Flutter-位置参数太多:需要2个,但找到3个.错误



我有一个代码如下:

import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:meta/meta.dart';
import 'package:timezone/timezone.dart' as tz;
part 'reminder_event.dart';
part 'reminder_state.dart';
class ReminderBloc extends Bloc<ReminderEvent, ReminderState> {
ReminderBloc() : super(ReminderInitial());
int? selectedRepeatDayIndex;
late DateTime reminderTime;
int? dayTime;
@override
Stream<ReminderState> mapEventToState(
ReminderEvent event,
) async* {
if (event is RepeatDaySelectedEvent) {
selectedRepeatDayIndex = event.index;
dayTime = event.dayTime;
yield RepeatDaySelectedState(index: selectedRepeatDayIndex);
} else if (event is ReminderNotificationTimeEvent) {
reminderTime = event.dateTime;
yield ReminderNotificationState();
} else if (event is OnSaveTappedEvent) {
_scheuleAtParticularTimeAndDate(reminderTime, dayTime);
yield OnSaveTappedState();
}
}
Future _scheuleAtParticularTimeAndDate(
DateTime dateTime, int? dayTime) async {
final flutterNotificationsPlugin = FlutterLocalNotificationsPlugin();
final androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your other channel id',
'your other channel name',
'your other channel description');
final iOSPlatformChannelSpecifics = IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics);
await flutterNotificationsPlugin.zonedSchedule(
1,
"Fitness",
"Hey, it's time to start your exercises!",
_scheduleWeekly(dateTime, days: _createNotificationDayOfTheWeek(dayTime)),
platformChannelSpecifics,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true,
matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime,
);
}
tz.TZDateTime _scheduleDaily(DateTime dateTime) {
final now = tz.TZDateTime.now(tz.local);
var timezoneOffset = DateTime.now().timeZoneOffset;
final scheduleDate = tz.TZDateTime.utc(now.year, now.month, now.day)
.add(Duration(hours: dateTime.hour, minutes: dateTime.minute))
.subtract(Duration(hours: timezoneOffset.inHours));
return scheduleDate.isBefore(now)
? scheduleDate.add(Duration(days: 1))
: scheduleDate;
}
tz.TZDateTime _scheduleWeekly(DateTime dateTime, {required List<int>? days}) {
tz.TZDateTime scheduleDate = _scheduleDaily(dateTime);
for (final int day in days ?? []) {
scheduleDate = scheduleDate.add(Duration(days: day));
}
return scheduleDate;
}
List<int> _createNotificationDayOfTheWeek(int? dayTime) {
switch (dayTime) {
case 0:
return [
DateTime.monday,
DateTime.tuesday,
DateTime.wednesday,
DateTime.thursday,
DateTime.friday,
DateTime.saturday,
DateTime.sunday
];
case 1:
return [
DateTime.monday,
DateTime.tuesday,
DateTime.wednesday,
DateTime.thursday,
DateTime.friday
];
case 2:
return [DateTime.saturday, DateTime.sunday];
case 3:
return [DateTime.monday];
case 4:
return [DateTime.tuesday];
case 5:
return [DateTime.wednesday];
case 6:
return [DateTime.thursday];
case 7:
return [DateTime.friday];
case 8:
return [DateTime.saturday];
case 9:
return [DateTime.sunday];
default:
return [];
}
}
}

然后我在网上得到了这个错误:final androidPlatformChannelSpecifics=AndroidNotificationDetails,看起来像:错误

我确实在analysis_options.yaml中添加了这行代码,但没有解决问题:

linter:
rules:
prefer_const_constructors: false

能请人帮忙吗?感谢

错误消息告诉您已经提供了3个位置参数,其中只需要两个。

如果您参考AndroidNotificationDetails类:https://pub.dev/documentation/flutter_local_notifications/latest/flutter_local_notifications/AndroidNotificationDetails-class.html

您将看到只有channleId和ChannelName参数是positionnal。其他的是名称争论。

所以正确的选择是:

final androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your other channel id',
'your other channel name',
channelDescription: 'your other channel description');

在AndroidNotificationDetails中,尝试删除一个参数。它在寻找两个论点,但你试图给出其中的三个。在最近的更新中,他们将争论从三个减少到两个。

如果您使用的是最新版本的flutter_local_notifications包,则说明参数不是位置参数。相反,您可以根据关键字channelDescription设置通道描述的值。

最好跟踪你正在使用的版本以及如何使用它的文档。包所有者为你的用例发布的示例可以在这里找到

最新更新