参数类型 'bool?' 无法分配给参数类型 'bool'

The argument type 'bool?' can't be assigned to the parameter type 'bool'

我正在开发一个 Flutter 应用程序,但我遇到了错误The argument type 'bool?' can't be assigned to the parameter type 'bool'.我如何解决它?

这是我的代码中出现错误的部分。我也 google 错误,他们说,我应该将 products[key], 更改为 () => products[key],。但这没有帮助。如果您需要更多信息,请询问。

void main() => runApp(MaterialApp(home: ToDo()));

class ToDo extends StatefulWidget {
  @override
  _ToDoState createState() => _ToDoState();
}

class _ToDoState extends State<ToDo> {
  Map<String, bool> products = {
    'ToDo': false, 'Java': false, 'Python': false, 'Schule': false, 'Hi': false
  };
  void addItem(String item) {
    setState(() {
      products[item] = false;
    });
    Navigator.of(context).pop();
  }
  
  void deleteItem(String key) {
    setState(() {
      products.remove(key);
    });
  }

  void toggleDone(String key) {
    setState(() {
      products.update(key, (bool done) => !done);
    });
  }

  void newEntry() {
    showDialog<AlertDialog>(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            content: TextField(
              onSubmitted: addItem,
            ),
          );
        }
      );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'ToDo-App',
          style: TextStyle(
              color: Colors.black,
              fontSize: 18.0,
              fontFamily: 'Opinehe',
              fontWeight: FontWeight.w400,
              decoration: TextDecoration.underline),
        ),
        backgroundColor: Color.fromRGBO(35, 152, 185, 100),
      ),
      body: ListView.builder(
        itemCount: products.length,
        itemBuilder: (context, i) {
          String key = products.keys.elementAt(i);
          return SingleToDo(
              key,
              products[key],
              () => deleteItem(key),
              () => toggleDone(key),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: newEntry,
        child: Icon(Icons.add),
      ),
    );
  }
}
class SingleToDo extends StatelessWidget {
  final String title;
  final bool done;
  final Function remove;
  final Function toggleDone;
  const SingleToDo(this.title, this.done, this.remove, this.toggleDone);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 22),
      child: ListTile(
        contentPadding: EdgeInsets.symmetric(vertical: 8.0),
        leading: Checkbox(
          value: done,
          onChanged: (bool? value) => toggleDone(),
        ),
        title: Text(
          title,
          style: TextStyle(
              fontSize: 18.0,
              fontWeight: FontWeight.w300,
              color: Colors.black54),
        ),
        trailing: IconButton(
          icon: Icon(Icons.delete_outline),
          onPressed: () => remove(),
        )
      ),
    );
  }
}

从获取值可以有 null,在您的模式下 class 需要不可为 null 的数据。

您可以检查值是否为空然后传递值,或者如果为空则使用默认值,例如

products[key] ?? false,

或使用 ! 如果您知道表达式的计算结果永远不会为 null products[key]!,

勾选 dart important-concepts