首页 > 解决方案 > 使用 Flutter 异步将数据写入云 Firestore

问题描述

我正在尝试在我的颤振应用程序中使用异步将数据写入 Firestore 集合中。

我确实有这个代码,但它似乎不起作用。

import 'package:location/location.dart';

Location _locationService = new Location();
  Future<void> setUserData() async {
    Map<String, dynamic> userData = <String, dynamic>{
      'email': this._emailController.text,
      'password': this._passwordController.text,
      'firstname': this._firstNameController.text,
      'familyname': this._familyNameController.text,
      'phonenumber': this._phoneNumberController.text,
      'neighbourhood': this._neighbourhoodController.text,
      'latitude': await _locationService.getLocation().latitude,
    };
    crudObj.addData(userData);
  }

crudObj.addData 直接写入我的 Firestore 集合。

我究竟做错了什么 ?

标签: fluttergoogle-cloud-firestore

解决方案


看起来您一开始还没有导入 cloud_firestore。

Future<void> setUserData() async {
Firestore.instance.collection(*name of collection*).document(*name of document*).setData(
    {'email': this._emailController.text,
 'password': this._passwordController.text, 
     'firstname': this._firstNameController.text,
    'familyname': this._familyNameController.text,
    'phonenumber': this._phoneNumberController.text,
    'neighbourhood': this._neighbourhoodController.text,
    'latitude': await _locationService.getLocation().latitude,});

}

我希望您将项目设置为连接到云 Firestore


推荐阅读