flutter - Flutter Todo 在 title = widget.title 处返回 null
问题描述
我的 Todolist 返回错误消息时遇到问题:“必须向文本小部件提供非空字符串。'package:flutter/src/widgets/text.dart':断言失败:第 360 行 pos 10: 'data != null'" 及其在类 MyHomePage 中的行标题:new Text(widget.title。我已经尝试将其更改为标题:new Text(widget.title??""),但这不起作用. 还有其他可行的解决方案吗?
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Todos',
theme: new ThemeData(primarySwatch: Colors.deepOrange),
home: new MyHomePage(title: 'Todos'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
MyHomePageState createState() => new MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: TodoList(),
);
}
}
class TodoList extends StatefulWidget {
@override
State<StatefulWidget> createState() => TodoListState();
}
class TodoListState extends State {
DbHelper helper = DbHelper();
List<Todo> todos;
int count = 0;
@override
Widget build(BuildContext context) {
if (todos == null) {
todos = List<Todo>();
getData();
}
return Scaffold(
body: todoListItems(),
floatingActionButton: FloatingActionButton(
onPressed: () {
navigateToDetail(Todo('', 3, ''));
},
tooltip: "Add new Todo",
child: new Icon(Icons.add)),
);
}
ListView todoListItems() {
return ListView.builder(
itemCount: count,
itemBuilder: (BuildContext context, int position) {
return Card(
color: Colors.white,
elevation: 2,
child: ListTile(
leading: CircleAvatar(
backgroundColor: getColor(this.todos[position].priority),
child: Text(this.todos[position].priority.toString()),
),
title: Text(this.todos[position].title),
subtitle: Text(this.todos[position].date),
onTap: () {
debugPrint("Tapped on " + this.todos[position].id.toString());
navigateToDetail(this.todos[position]);
},
),
);
},
);
}
void getData() {
final dbFuture = helper.initializeDb();
dbFuture.then((result) {
final todosFuture = helper.getTodos();
todosFuture.then((result) {
List<Todo> todoList = List<Todo>();
count = result.length;
for (int i = 0; i < count; i++) {
todoList.add(Todo.fromObject(result[i]));
debugPrint(todoList[i].title);
}
setState(() {
todos = todoList;
count = count;
});
debugPrint("Items " + count.toString());
});
});
}
Color getColor(int priority) {
switch (priority) {
case 1:
return Colors.red;
break;
case 2:
return Colors.orange;
break;
case 3:
return Colors.green;
break;
default:
return Colors.green;
}
}
void navigateToDetail(Todo todo) async {
bool result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => TodoDetail(todo)),
);
if (result == true) {
getData();
}
}
}
解决方案
请将此行更改title: new Text(widget.title),
为title: new Text(widget.title??""),
first ?? ""
这是一个空检查...当第一个为空时返回""
编辑
请对所有文本小部件重复这种方式
child: Text(this.todos[position].priority.toString() ?? ""),
title: Text(this.todos[position].title ?? "" ),
subtitle: Text(this.todos[position].date ?? "" ),
推荐阅读
- ruby-on-rails - Couldn't find Category without an ID for Rails Blog
- ios - 快速删除结构数组
- vb.net - vb.net 将文件添加到现有项目
- javascript - 将复选框作为二进制值(“0”表示未选中,“1”表示选中)转换为 json 数据
- python - 程序类被卡住/空闲,并且在 Anaconda/命令行提示中的第一次调用后不执行剩余的调用,但在 Spyder 中工作
- c# - 为什么我的状态机抛出错误:无效转换?
- android - FirebaseUser currentUser 正在显示数组
- image - Instagram应用内浏览器不加载图像
- reactjs - componentDidMount 之外的异步反应
- html - 我可以对具有 start 属性值的有序列表进行样式编号吗?