简体   繁体   中英

Can we use 'Firestore' like this?

I downloaded flutter chat app on Github and I was learning through it. The original developer used Firestore but in mine, I am getting an error "Undefined name 'Firestore'. Try correcting the name to one that is defined, or defining the name" like this.

I search for this and I read in cloudfirestore docs we can use "FirebaseFirestore" (maybe I am wrong). I am learning to write backend with flutter and so far I did UI parts. so this is my first attempt at learning backend with flutter.

handleSignIn() async {
    final res = await googleSignIn.signIn();

    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

    final auth = await res.authentication;

    final credentials = GoogleAuthProvider.getCredential(
        idToken: auth.idToken, accessToken: auth.accessToken);

    final firebaseUser =
        (await firebaseAuth.signInWithCredential(credentials)).user;

    if (firebaseUser != null) {
      final result = (await Firestore.instance
              .collection('users')
              .where('id', isEqualTo: firebaseUser.uid)
              .getDocuments())
          .documents;

      if (result.length == 0) {
        ///new user
        Firestore.instance
            .collection('users')
            .document(firebaseUser.uid)
            .setData({
          "id": firebaseUser.uid,
          "name": firebaseUser.displayName,
          "profile_pic": firebaseUser.photoURL,
          "created_at": DateTime.now().millisecondsSinceEpoch,
        });

        sharedPreferences.setString("id", firebaseUser.uid);
        sharedPreferences.setString("name", firebaseUser.displayName);
        sharedPreferences.setString("profile_pic", firebaseUser.photoURL);

        Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => Home()));
      } else {
        ///Old user
        sharedPreferences.setString("id", result[0]["id"]);
        sharedPreferences.setString("name", result[0]["name"]);
        sharedPreferences.setString("profile_pic", result[0]["profile_pic"]);

        Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => Home()));
      }
    }
  }

this is the error I'm getting在此处输入图像描述

so can you guys explain how to solve this error?

Thank You.

I think you are missing to add cloud_firestore dependency in pubspec.yml. One more thing,

you are using latest version, use "FirebaseFirestore" instead of "Firestore".

  • Go to pubspec.yaml file in your flutter project. Add this( cloud_firestore: ^0.16.0 ) below dependencies like in the image below.

  • You can skip this step if you use VScode. It does this automatically if you save the file after the update. IF NOT , do flutter pub get inside your project. (this fetches the new packages added to the project, in our case cloud_firestore )

  • Make sure that you are able to see this import in the file you are getting the errors in.

import 'package:cloud_firestore/cloud_firestore.dart';

  • Next, update Firestore.instance -> FirebaseFirestore.instance in all your files where you are getting this error.

在此处输入图像描述

Use this code --

FirebaseFirestore.instance
        .collection("users").add({"name": "Majeed"});

change Firestore with FirebaseFirestore and remove the Document and setData replace setData with add.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM