如何为列表中的所有值添加复选框

问题描述 投票:0回答:2

我正在尝试将复选框添加到列表中的所有值我需要单独获取列表中每个人的值下面的代码在onchange(boolval)中说:

“bool?”类型的值无法分配给“bool”类型的变量。⏎尝试更改变量的类型,或将右侧类型转换为“bool”。

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class TestFile extends StatefulWidget {
  const TestFile({super.key});

  @override
  State<TestFile> createState() => _TestFileState();
}

class _TestFileState extends State<TestFile> {
  Map<String, bool> values = {
    'foo': true,
    'bar': false,
  };
  List<dynamic> items = [];

  String? _selectedItem;
  bool _ischecked = false;
  DateTime _selectedDate = DateTime.now();
  List<String> myList = [];
  List<String> checklist = [];
  //!database reference and query
  final auth = FirebaseAuth.instance;
  final ref = FirebaseDatabase.instance.ref('attendance').child('studentlist');

  //!

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.purple,
        bottom: PreferredSize(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                child: Padding(
                  // padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),

                  padding: const EdgeInsets.only(left: 16.0),
                  child: DropdownButton<String>(
                    value: _selectedItem,
                    onChanged: (String? newValue) {
                      setState(() {
                        _selectedItem = newValue;
                      });
                    },
                    items: <String>[
                      'period 1',
                      'period 2',
                      'period 3',
                      'period 4',
                      'period 5',
                      'period 6',
                      'period 7',
                    ].map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,
                        child: Text(value),
                      );
                    }).toList(),
                    icon: Icon(Icons.arrow_drop_down),
                    iconSize: 40,
                    underline: Container(),
                    iconEnabledColor: Colors.white, //Icon color
                    style: TextStyle(
                        //te
                        color: Colors.white, //Font color
                        fontSize: 20 //font size on dropdown button
                        ),
                    dropdownColor: Colors.purple,
                  ),
                ),
              ),
              SizedBox(
                width: 100,
              ),
              Container(
                child: Center(
                  child: IconButton(
                    icon: Icon(Icons.calendar_month, color: Colors.white),
                    onPressed: () async {
                      final DateTime? picked = await showDatePicker(
                        context: context,
                        initialDate: _selectedDate,
                        firstDate: DateTime(1900),
                        lastDate: DateTime.now(),
                      );
                      if (picked != null && picked != _selectedDate) {
                        setState(() {
                          _selectedDate = picked;
                        });
                      }
                    },
                  ),
                ),
              ),
              SizedBox(width: 5),
              Text(
                DateFormat('dd/MM/yyyy').format(_selectedDate),
                style: TextStyle(
                    //te
                    color: Colors.white, //Font color
                    fontSize: 20 //font size on dropdown button
                    ),
              ),
            ],
          ),
          preferredSize: Size.fromHeight(30.0),
        ),
      ),
      body: Column(
        children: [
          Expanded(
            child: StreamBuilder(
              stream: ref.onValue,
              builder: (context, AsyncSnapshot<DatabaseEvent> snapshot) {
                List<bool> checked = List<bool>.filled(items.length, false);
                if (!snapshot.hasData) {
                  return CircularProgressIndicator();
                } else {
                  Map<dynamic, dynamic> map =
                      snapshot.data!.snapshot.value as dynamic;
                  items.clear();
                  items = map.values.toList();
                  return ListView.builder(
                    itemCount: snapshot.data!.snapshot.children.length,
                    itemBuilder: (context, Index) {
                      return new ListView(
                        children: values.keys.map((String key) {
                          return new CheckboxListTile(
                              title: Text(items[Index]['sname'],
                                  style: TextStyle(
                                      fontSize: 19,
                                      fontWeight: FontWeight.w500)),
                              value: values[key],
                              onChanged: (  val) {
                                setState(() {
                                   values[key] = val;
                                  // if(values!=null){
                                  //   values[key] = val;
                                  // }else{
                                  //   values[key] = false;
                                  // }
                                  
                                });
                              });
                        }).toList(),
                      );
                    },
                  );
                }
              },
            ),
          ),
        ],
      ),
      floatingActionButton: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            padding: EdgeInsets.fromLTRB(28, 0, 0, 0),
            child: FloatingActionButton.extended(
              onPressed: () {
                for (int i = 0; i < items.length; i++) {
                  print(items[i]);
                }
                // Add your onPressed code here!
              },
              label: const Text('ADD'),
              icon: const Icon(Icons.add),
              backgroundColor: Colors.purple,
            ),
          ),
        ],
      ),
    );
  }
}
flutter firebase firebase-realtime-database checkboxlist
2个回答
1
投票

如果答案似乎发生在这里:

values[key] = val;

报错信息说你的

val
bool?
类型,而你的
values[key]
bool
类型。

这意味着

val
可以是
true
false
null
,而
values[key]
只能是
true
false
。所以你需要确定当
val
null
时你想做什么。

这样做的一种方法是检查该值,并在这种情况下跳过分配:

if (val != null) {
  values[key] = val!;
}
else {
  print('val is null, skipping assignment');
}

0
投票

问题是由 CheckboxListTileonChanged 回调中的可为空的 bool 值引起的。回调函数应该接收一个不可为 null 的 bool 值。要修复错误,请将回调中的 val 的类型更改为 bool 而不是 bool?。

这是更新后的代码:

// updated 

return new ListView(
  children: values.keys.map((String key) {
    return new CheckboxListTile(
        title: Text(items[Index]['sname'],
            style: TextStyle(
                fontSize: 19,
                fontWeight: FontWeight.w500)),
        value: values[key],
        onChanged: (bool val) { // Changed to "bool val" instead of "bool? val"
          setState(() {
            values[key] = val;
          });
        });
  }).toList(),
);

通过此更改,错误应该得到解决,您的代码应该可以正常工作。

PS:如果您觉得这个答案相关且有帮助,请不要忘记点赞*

© www.soinside.com 2019 - 2024. All rights reserved.