简体   繁体   English

如何使用 dart 从 firebase 中的文档访问字段数据

[英]How to access the field data from a document in firebase using dart

这是数据库

i want to store the field value course name and use it further,我想存储字段值课程名称并进一步使用它,

i tried this but it didnt work giving error as我试过这个,但它没有工作给出错误

Unhandled Exception: Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist未处理的异常:错误 state:无法获取 DocumentSnapshotPlatform 上不存在的字段

    print(widget.scourse);
     DocumentSnapshot dsa = await FirebaseFirestore.instance
              .collection("users")
              .doc("$widget.scourse")
              .get();
 
      print(dsa.get('courseName'));
    // final studentsc=course.toString().toLowerCase().split(" ").join() +"student";
    
   }        

widget.scourse is the document name widget.scourse 是文档名称

this is the code i had written pl help这是我写的代码请帮助

If all things are working (with your database, setup, etc.), there seems to be an issue with the doc id in your code.如果一切正常(使用您的数据库、设置等),那么您的代码中的 doc id 似乎存在问题。

When you want to access a variable in a string, putting a dollar sign in front of it is okay.当你想访问字符串中的变量时,在它前面放一个美元符号是可以的。 But when you want to do any computation or complex variable access (like you are doing), you have to wrap the computation in curly braces.但是当您想要进行任何计算或复杂的变量访问时(就像您正在做的那样),您必须将计算包装在花括号中。

The following code should explain what I mean:下面的代码应该解释我的意思:

// this is the simple normal way
const foo = true;
print('$foo is true'); // true is true

// but once you need some computation or complex variable access,
// you need to include the computation in curly braces,
// immediately after the dollar sign
const bar = [1];
const h24 = Duration(hours: 24);
print('${bar[0] * 2} is 2'); // 2 is 2
print('${h24.inHours} is 24'); // 24 is 24

// the following won't give you what you want
print('$bar[0] * 2 is 2'); // [1][0] * 2 is 2
print('$h24.inHours is 24'); // 24:00:00.000000.inHours is 24

Now coming over to the code you shared, you mentioned that widget.scourse is the document name (I suppose id here).现在来看您共享的代码,您提到 widget.scourse 是文档名称(我想这里是 id)。 Accessing it inside the.doc method, you should use the braces like I explained above.在 .doc 方法中访问它,你应该像我上面解释的那样使用大括号。 Something, like the following:一些东西,如下所示:

DocumentSnapshot dsa = await FirebaseFirestore.instance
  .collection("users")
  .doc("${widget.scourse}")
  .get();

Or better still, since widget.scourse is a string already, you don't need to put it inside quotes again (and access it with dollar sign), you simply use it directly.或者更好的是,因为 widget.scourse 已经是一个字符串,您不需要再次将它放在引号内(并使用美元符号访问它),您只需直接使用它。

DocumentSnapshot dsa = await FirebaseFirestore.instance
  .collection("users")
  .doc(widget.scourse)
  .get();

Here you missed the curly brackets so.scourses was sent as string instead of its value在这里你错过了大括号所以.scourses 作为字符串而不是它的值发送

doc("${widget.scourse}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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