flutter - 错误:没有为“TasksScreen”类型定义方法“setState”。([todoey] lib/screens/tasks_screen.dart:26 处的未定义方法)
问题描述
所以我正在尝试使用回调将任务添加到我的任务列表中。但我无法在我的回调函数中访问 setState()。让我解释,
这是我的 add_task_screen,其中包含我的 modalBottomSheet。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class AddTaskScreen extends StatelessWidget {
String newTaskText;
final Function addNewTask;
AddTaskScreen({@required this.addNewTask});
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Padding(
padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
child: Container(
color: Color(0xFF757575),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.circular(20.0),
)
),
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 65.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15.0),
child: Text(
'Add Task',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.lightBlueAccent,
fontSize: 30.0
),
),
),
TextField(
style: TextStyle(fontSize: 22.0 ),
autofocus: true,
textAlign: TextAlign.center,
onChanged: (value) {
newTaskText = value;
},
),
SizedBox(
height: 15.0,
),
FlatButton(
color: Colors.lightBlueAccent,
child: Text(
'Add',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
onPressed: () {
addNewTask(newTaskText);
},
)
],
),
),
),
),
),
);
}
}
所以在这里,当我点击“添加”时,我想将我的任务添加到我的任务列表中。但使用回调,只是为了更新我的应用程序的其他部分。所以我在任务屏幕中使用回调,它是 add_tasks 页面的父级。这是我的 tasks_screen 页面,
import 'package:flutter/material.dart';
import 'package:todoey/widgets/tasks_list.dart';
import 'package:todoey/screens/add_task_screen.dart';
import 'package:todoey/models/task.dart';
class TasksScreen extends StatelessWidget {
List<Task> tasks = [];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.lightBlueAccent,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.lightBlueAccent,
child: Icon(Icons.add, size: 40.0),
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => AddTaskScreen(
addNewTask: (String newTaskText) {
print(newTaskText);
tasks.add(Task(taskText: newTaskText));
// FIXME: Some issue here...
setState(() {
tasks.add(Task(taskText: newTaskText));
});
Navigator.pop(context);
},
),
isScrollControlled: true
);
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 60.0, left: 30, right: 30.0, bottom: 30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
CircleAvatar(
child: Icon(Icons.list, size: 30.0, color: Colors.lightBlueAccent,),
backgroundColor: Colors.white,
radius: 30.0,
),
SizedBox(
height: 10.0,
),
Text(
'Todoey',
style: TextStyle(
color: Colors.white,
fontSize: 50.0,
fontWeight: FontWeight.w700,
),
),
Text(
'12 Tasks',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
],
),
),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(20.0),
topLeft: Radius.circular(20.0)
),
),
child: TasksList(tasks: tasks),
),
),
],
),
);
}
}
所以我在 FIXME 评论下的 setState() 上收到错误。错误是问题的标题。因此,具有讽刺意味的是,任务正在打印,在我的控制台中。但是,当我尝试更改状态以在我的任务列表中添加一个任务然后弹出底部表单时,我收到了一个错误。注意:任务是一个模型。
请有人告诉我如何解决这个问题。提前致谢。
解决方案
必须将 t.ask_screen 转换为有状态的小部件 [已解决]
推荐阅读
- php - 仅在 WooCommerce 前端中使用 gettext 过滤器进行字符串翻译
- reactjs - 如何在 React js 中使用调度获取异步数据
- c++ - 在 C++ 中反转整数
- arrays - Shopify Liquid - 部分块不可分页
- rust - 无法推断在关联函数上声明的类型参数“I”的类型
- ios - 如何快速过滤日期数组
- c - 我想确定每个“$”和“T”之间是否至少有一个“G”
- kendo-ui - How to implement dropdown list in a grid for kendoUI for angular. Can't find any documentation
- python - 无法在 Django Rest Framework 中发布数据
- date - 可以在不更改 conf 文件的情况下更改 RSYSLOG_FileFormat 时间戳输出吗?