gpt4 book ai didi

firebase - cloud_firestore 0.14.0如何使用数据方法

转载 作者:行者123 更新时间:2023-12-03 04:12:56 24 4
gpt4 key购买 nike

我更新为cloud_firestore 0.14.0
有一些中断更改:

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


在更新之前,这就是我使用 setter/getter data的方式
之前:
Future<String> getUsernameFromUserId(String userId) async {
return (await _userFirestoreCollection.document(userId).get()).data['screenName'];
}
这就是我现在使用数据的方式。但是似乎不起作用...
现在:
Future<String> getUsernameFromUserId(String userId) async {
return (await _userFirestoreCollection.document(userId).get()).data()['screenName'];
}

最佳答案

cloud_firestore 0.14.0版本开始:document()方法已更改,现在您必须改为使用doc(),因此在代码中必须执行以下操作:

    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();
}
}

关于firebase - cloud_firestore 0.14.0如何使用数据方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63486340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com