如何使用嵌套双精度数据将数据添加到Firebase

时间:2020-05-19 10:52:26

标签: firebase flutter dart google-cloud-firestore

我正在尝试将数据添加到Firestore。 但是在此之前,第二个mileage值始终为null。 字符串值很好。

    String nickName;
    double mileage;

          InputFieldTile(
            label: 'Nick Name',
            keyboardType: TextInputType.text,
            onChanged: (value) {
              nickName = value;
            },
          ),
          InputFieldTile(
            label: 'Mileage(km)',
            keyboardType: TextInputType.number,
            onChanged: (value) {
              mileage = value.toDouble();
            },
          ),

              _firestore.collection('Users').add({
                'nick_name': nickName,
                'data': {
                  'mileage': 1234567,
                  'dummy': 'Dummy'
                }
              });

class InputFieldTile extends StatelessWidget {
  InputFieldTile({this.label, this.keyboardType, this.onChanged});

  final String label;
  final TextInputType keyboardType;
  final Function onChanged;

  TextEditingController controller = TextEditingController();

  String onChangeCallBack(value) {
    return controller.text;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ListTile(
        leading: Text(
          label,
          style: TextStyle(
            color: Colors.white,
          ),
        ),
        title: TextFormField(
          controller: controller,
          onChanged: onChanged,
          style: TextStyle(color: Colors.white),
          textAlign: TextAlign.right,
          cursorColor: Colors.white,
          keyboardType: keyboardType,
          decoration: InputDecoration(
            focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.white),
            ),
          ),
        ),
      ),
    );
  }
}

这有什么问题?

1 个答案:

答案 0 :(得分:2)

也许这会起作用:

InputFieldTile(
            label: 'Mileage(km)',
            keyboardType: TextInputType.number,
            onChanged: (value) {
              mileage = double.parse(value);
            },
          ),