简体   繁体   中英

Flutter ToDo list checkbox cant assign bool

Im trying to develop a ToDo list with flutter.

Im running a Stateful Widget for the list, the build method for its state looks like this:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('ToDo App'),
        backgroundColor: const Color.fromRGBO(35, 0, 0, 100),
      ),
      body: ListView.builder(
          itemCount: products.length,
          itemBuilder: (context, index) {
            String key = products.keys.elementAt(index);
            return ToDoListEntry(
                key, products[key], () => deleteItem(key), () => check(key));
          }),
      floatingActionButton: FloatingActionButton(
        onPressed: newEntry,
        child: const Icon(Icons.arrow_downward),
      ),
    );
  }
}

a function addItem(String item) is working, also a function deleteItem(String key), i pass to the ToDoListEntry class is working. Now i tried to code updating functionality for the Checkbox and saved my entries into the Map<String, bool> products:

Map<String, bool> products = {
    'Kartoffel': false,
    'Tomate': false,
    'Käse': false,
    'Wurst': false
  };

When i now passed products[key] to my ToDoListEntry Widget it says: "The argument type 'bool?' can't be assigned to the parameter type 'bool'"

What is 'bool?' i cant find any explainations.

ToDoListEmtry looks like this:

class ToDoListEntry extends StatelessWidget {
  final String title;
  bool state;
  final Function remove, check;
  ToDoListEntry(this.title, this.state, this.remove, this.check);

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

Here i get problems with the onChecked method in Checkbox: "The argument type 'void Function(bool)' can't be assigned to the parameter type 'void'"

The reference to the map entry products[key] must be trailed by the, operator. to guarantee that it won't be null.

return ToDoListEntry(
  key, products[key]!, () => deleteItem(key), () => check(key));
}),

The onChanged method for my Checkbox must be nullsafe for some reason. The error doesn't point that out.

leading: Checkbox(
          value: state,
          onChanged: (bool? value) => check(),
        ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM