【问题标题】:firebase bool passes to null bugfirebase bool 传递给 null 错误
【发布时间】:2020-06-16 03:44:38
【问题描述】:

您好,当我单击按钮从 true 到 false 并从 false 到 true 时,我试图更改 firebase 中的数据,但是当我单击数据库中的值时,它变为 null。 有谁知道出了什么问题?谢谢。

class _MyHomePageState extends State<MyHomePage> {
  bool selected;
  @override
  void initState() {
    super.initState();
    setInitialValue();
  }
  setInitialValue() async {
    final value = await _incrementCounter();
    setState(() {
      selected = value?? true;
    });
  }
  _loadCounter(bool value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('select', value);
  }
  Future<bool> _incrementCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool boolValue = prefs.getBool('select');
    return boolValue;
  }
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Column(
          children: <Widget>[
            Padding(
                padding: const EdgeInsets.all(8.0),
                child: Center(child: Text('ola'))),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              child: Text( 'Add', style: TextStyle(color: Colors.white), ),
              onPressed: () {
                Firestore.instance.collection('teste').document('eHJzjbq6c7MwrObrR9XY').updateData({'mover' : selected});
                }
            ),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    似乎selected 变量的值设置不正确,因此当您尝试更新文档时,您发送的是空值。尝试验证 selected 的值,以检查它是否确实具有布尔值,然后再将其写入 Firestore。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多