首页 > 解决方案 > 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();
    }
  }
}

标签: flutterdart

解决方案


请将此行更改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 ?? "" ),

推荐阅读