方法"jumpToDay"在null上被调用. [扑] [英] The method 'jumpToDay' was called on null. [Flutter]
问题描述
在此应用程序中,用户将能够添加任务并将其显示在PageView.builder
中.用户也可以通过向左或向右滑动来滚动PageView.builder
.浏览量映射到日期(例如:当前页面映射到今天的日期,下一页映射到明天的日期,等等).我还想实现一个跳转功能,该功能允许用户通过在showDatepicker
小部件上指定日期来移动到新页面.
In this application, the user will be able to add tasks and display it in a PageView.builder
. The user will also be able to scroll through the PageView.builder
by swiping left or right. The pageview is mapped to dates (ex: current page maps to today's date, next page maps to tomorrow's date, etc.). I also wanted to implement a jump feature which lets the user move to a new page by specifying a date on a showDatepicker
widget.
PageView.builder
是通过使用此项目实现的.该项目还具有跳转到页面的功能: https://github.com/ZedTheLed/calendar_views
The PageView.builder
was implemented by using this project. this project also has the jump to page feature : https://github.com/ZedTheLed/calendar_views
showDatepicker
通过以下方法实现.通过点击Raisedbutton
来调用它:
the showDatepicker
is implemented in the method below. it is called by clicking on a Raisedbutton
:
_selectDate() async {
final DateTime picker =await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2010),
lastDate: DateTime(2040)
);
if (picker != null) {
print(picker);
return _daysPageController.jumpToDay(picker); // this method jumps the user to a selected date
// Navigator.pop(context);
// print(selectedDate);
// await runJump(selectedDate);
// return selectedDate;
}
}
当用户单击日期时,变量DateTime picker
成功返回用户在打印语句中选择的日期.但是,当我将此值传递给jumptopage方法时,它给出了他的错误:The method 'jumpToDay' was called on null.
when the user clicks on a date, the variable DateTime picker
successfully returns the user-selected date in the print statement. But when i pass this value to the jumptopage method, it gives his error : The method 'jumpToDay' was called on null.
PageView.builder
在以下代码中实现:
final List<DateTime> days;
Scaffold(
floatingActionButton: new RaisedButton(
child: new Text("Jump To Today"),
onPressed: () {
_selectDate();
},
),
body: new Column(
children: widget.days.map(((day) => Container(
constraints: new BoxConstraints(
maxHeight: MediaQuery.of(context).size.height * 1.00,
maxWidth: MediaQuery.of(context).size.width * 1.00
),
child: Starting_screen(_makeTextString(day)), //this screen displays the tasks added by the user
)
),
).toList()
),
);
方法_makeTextString
只是修改了DateTime
值,并以理想的格式返回了它.
the method _makeTextString
just modifies a DateTime
value and returns it in a desirable format.
我可以对如何处理此错误提出建议吗?
Could i get a suggestion on how to handle this error?
完整的项目在这里可用: https://bitbucket.org/NotAnurag/todolist_restarted/src/master/
full project is available here : https://bitbucket.org/NotAnurag/todolist_restarted/src/master/
推荐答案
当用户单击日期时,变量
DateTime
选择器将在打印语句中成功返回用户选择的日期.但是当我将此值传递给jumptopage
方法时,它给出了他的错误:方法jumpToDay
被调用为空"
When the user clicks on a date, the variable
DateTime
picker successfully returns the user-selected date in the print statement. But when I pass this value to thejumptopage
method, it gives his error: "The methodjumpToDay
was called on null"
我在代码中看到的问题是_daysPageController
未初始化. IDE有助于建议您可以在DaysPageController
上调用的各种方法,但事实是声明了它,但是没有为其赋值(意味着它是null
):
The problem that I see in the code is that _daysPageController
is not initialized. IDE helpfully suggests various methods that you can call on DaysPageController
, but the fact remains that it is declared, but no value is assigned to it (meaning that it is null
):
DaysPageController _daysPageController; // remains null unless assigned a value
因此,错误试图告诉您的是在null
上调用了jumpToDay(DateTime)
(显然没有这种方法).如果您查看堆栈跟踪,那里会隐含一些更明确的提示:
Therefore, what the error is attempting to tell is that jumpToDay(DateTime)
is called on null
(which obviously does not have such a method). If you look at the stack trace a bit more explicit hint is buried in there:
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)]未处理的异常:NoSuchMethodError:方法'jumpToDay'在null上调用.
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'jumpToDay' was called on null.
尝试初始化控制器,看看它是否有助于解决问题.
Try to initialize the controller and see if it helps to resolve the issue.
这篇关于方法"jumpToDay"在null上被调用. [扑]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!