cloud_firestore 0.14.0 how to use the data method

后端 未结 1 1240
情深已故
情深已故 2020-12-04 04:01

I updated to cloud_firestore 0.14.0

There\'s a few break changes:

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

相关标签:
1条回答
  • 2020-12-04 04:31

    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"];
        }
    

    Example:

    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();
      }
    }
    
    0 讨论(0)
提交回复
热议问题