简体   繁体   中英

Unhandled Exception: Bad state: cannot get a field on a DocumentSnapshotPlatform which does

在此处输入图像描述 I want to display the current user name on my app.

I tried this but got the error

Unhandled Exception: Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist

Future getUserData() async {
    User? user = await FirebaseAuth.instance.currentUser;
    final DocumentSnapshot doc = await FirebaseFirestore.instance
        .collection("UserData")
        .doc(user!.uid)
        .get();

    name = doc['name'];
    print("name $name");
  }

Then, I tried this:

  Future getUserData() async {
    User? user = await FirebaseAuth.instance.currentUser;
    try {
      final doc = await FirebaseFirestore.instance
          .collection("UserData")
          .doc(user!.uid)
          .get();

      final ds = await doc.get();
      final data = ds.data() as Map<String, dynamic>;

      name = data['name'];
      print("name $name");
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

But it shows an error for the doc.get()

1 positional argument(s) expected, but 0 found. Try adding the missing arguments

What can I do?

.get method expect a key('String') of filed/filePath.

  final doc = await FirebaseFirestore.instance
      .collection("UserData")
      .doc(user!.uid)
      .get();

  final name = await doc.get("name");
  print("name $name");

Also make sure the project setup is ok. More about reading data .

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