首页 > 解决方案 > 错误:在 null 上调用了方法“round”。接收方:null 尝试调用:round()。构建小部件时抛出错误

问题描述

在我的程序中,我从 cloud firestore 中提取值作为初始值,并希望它们在滑块的情况下对用户输入做出反应。但是,会抛出上述错误。我没有发布整个班级,因为它没有让我。只是保持它的必需品。如果您需要更多信息,请告诉我。提前致谢!

这是我的代码:


class _ListPlusScreenState extends State<ListPlusScreen> {
  @override
  double _progress;
  
  String _name;

  Widget _BuildListItem(BuildContext context, DocumentSnapshot doc) {
    _progress = doc['Progress'];
    final _name = doc['Name'].toString();
    final _due = doc['Due'];
    final _priority = doc['Priority'];
    DocumentReference _listRef =
        Firestore.instance.collection("List Items").document();

    return Container(
      padding: EdgeInsets.all(15),
      width: 200,
      height: 600,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20),
        color: Color(0xffacb3b8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                _name,
                style: TextStyle(fontSize: 20),
              ),
              Checkbox(value: false, onChanged: null)
            ],
          ),
          Slider(
            value: _progress,
            min: 0.0,
            max: 100.0,
            divisions: 10,
            label: _progress.round().toString(),
            onChanged: (value) {
              setState(
                () {
                  _progress = value;
                  _listRef.updateData({'Progress': value});
                },
              );
            },
          ),
        ],
      ),
    );
  }

  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      decoration: BoxDecoration(
        color: const Color(0xFF6EC6CA),
        shape: BoxShape.rectangle,
        borderRadius: BorderRadius.circular(20.0),
      ),
      child: StreamBuilder(
          stream: Firestore.instance.collection('List Items').snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return const Text('Loading..');
            return ListView.builder(
              itemExtent: 150,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) =>
                  _BuildListItem(context, snapshot.data.documents[index]),
            );
          }),
    );
  }
}

            ```

标签: firebasefluttergoogle-cloud-firestore

解决方案


double _progress =0.0;
label: _progress?.round().toString()??"0.0",

更改代码中的两行代码


推荐阅读