skip to Main Content

This is the databse

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

    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

this is the code i had written pl help

2

Answers


  1. Here you missed the curly brackets so .scourses was sent as string instead of its value

    doc("${widget.scourse}")
    
    Login or Signup to reply.
  2. If all things are working (with your database, setup, etc.), there seems to be an issue with the doc id in your code.

    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). Accessing it inside the .doc method, you should use the braces like I explained above. 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.

    DocumentSnapshot dsa = await FirebaseFirestore.instance
      .collection("users")
      .doc(widget.scourse)
      .get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search