cloud_firestore 0.14.0 如何使用数据方法 [英] cloud_firestore 0.14.0 how to use the data method

查看:24
本文介绍了cloud_firestore 0.14.0 如何使用数据方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更新到了 cloud_firestore 0.14.0

I updated to cloud_firestore 0.14.0

有一些中断更改:

突破:get data getter 现在是一个 data() 方法.

BREAKING: The get data getter is now a data() method instead.

在更新之前这是我如何使用getter data

Before the update this is how I used the getter data

之前:

Future<String> getUsernameFromUserId(String userId) async {
        return (await _userFirestoreCollection.document(userId).get()).data['screenName'];
    }

这就是我现在使用数据的方式..但似乎不起作用......现在:

This is how I use data now.. But doesn't seem to be working... Now:

Future<String> getUsernameFromUserId(String userId) async {
            return (await _userFirestoreCollection.document(userId).get()).data()['screenName'];
        }

推荐答案

从版本cloud_firestore 0.14.0开始:

方法 document() 已更改,现在您必须改用 doc(),因此在您的代码中,您必须执行以下操作:

Starting from version cloud_firestore 0.14.0:

The method document() was changed and now you have to use doc() instead, therefore in your code you have to do the following:

    Future<String> getUsernameFromUserId(String userId) async {
        return (await _userFirestoreCollection.doc(userId).get()).data()["screenName"];
    }

示例:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FirstRoute(title: 'First Route'),
    );
  }
}

class FirstRoute extends StatefulWidget {
  FirstRoute({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _FirstRouteState createState() => _FirstRouteState();
}

class _FirstRouteState extends State<FirstRoute> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("test"),
        ),
        body: FutureBuilder(
          future: getData(),
          builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return Column(
                children: [
                  Container(
                    height: 27,
                    child: Text(
                      "Name: ${snapshot.data.data()['name']}",
                      overflow: TextOverflow.fade,
                      style: TextStyle(fontSize: 20),
                    ),
                  ),
                ],
              );
            } else if (snapshot.connectionState == ConnectionState.none) {
              return Text("No data");
            }
            return CircularProgressIndicator();
          },
        ));
  }

  Future<DocumentSnapshot> getData() async {
    await Firebase.initializeApp();
    return await FirebaseFirestore.instance
        .collection("users")
        .doc("docID")
        .get();
  }
}

这篇关于cloud_firestore 0.14.0 如何使用数据方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆