小编flu*_*g98的帖子

Flutter:在 null 上调用了方法“add”

所以我创建了一个简单的通知类,如下所示:

class NamedNotification{
  String title;
  DateTime scheduledDate;
  int id;
  String description;

  NamedNotification(this.scheduledDate, this.id, this.title, this.description);
}
Run Code Online (Sandbox Code Playgroud)

我使用这个类来跟踪我的通知,并在创建新通知时将它们添加到列表中。我这样做:

List<NamedNotification> notifications;

onPressed: () {
    setState(() {
        NotificationPlugin newNotification = NotificationPlugin(); 
        newNotification.showAtDayAndTimeNotification(_dateTime, id, title, description);
        NamedNotification listNotification = NamedNotification(_dateTime, id, title, description);
        print(listNotification);
        notifications.add(listNotification);
        id++;
    });
    print(id);
    Navigator.pop(context);
},

Run Code Online (Sandbox Code Playgroud)

但是,每当我尝试将我的 namedNotification 添加到列表中时,我都会收到此错误:

The method 'add' was called on null.
Receiver: null
Tried calling: add(Instance of 'NamedNotification')
Run Code Online (Sandbox Code Playgroud)

我想知道解决这个问题的最佳方法是什么。

list dart flutter

3
推荐指数
1
解决办法
4876
查看次数

如何将 CupertinoDatePicker 保持在屏幕底部而不是整个屏幕?

所以,我在我的应用程序中实现了一个 CupertinoDatePicker,但是当我尝试访问它时,它显示在整个屏幕上,而不仅仅是底部。

这是代码:

onPressed: () {
 showCupertinoModalPopup(
 context: context,
 builder: (BuildContext context) => CupertinoDatePicker(
  mode: CupertinoDatePickerMode.dateAndTime,
  onDateTimeChanged: (DateTime date) {
   _dateTime = date;
   }),
  );
},
Run Code Online (Sandbox Code Playgroud)

我想知道为什么会这样。参考文档也没有真正的帮助。

flutter flutter-layout

2
推荐指数
1
解决办法
2726
查看次数

标签 统计

flutter ×2

dart ×1

flutter-layout ×1

list ×1