Define getter Mongo_dart Flutter

188 Views Asked by At

why does it say "The getter 'fieldName' isn't defined for the type 'Map<String, dynamic>'" when I would have to return a fieldname.

import 'package:mongo_dart/mongo_dart.dart';

 var db = Db("mongodb://127.0.0.1:27017/test"); 
 await db.open();
 var collection = db.collection('Users');     
 await collection.find().forEach(
             (users) {
               dbfieldName = users.fieldName; // This is where the problem occurs.
             },
           );

Thanks in advance.

1

There are 1 best solutions below

0
On

The method find retrieves many documents, so any users that you get is a document (in Map format). Now I have not clear what you want to do with fieldName. If you want all field names for the document users you can do:

print(users.keys);

if you want the value of the field 'foo' you have to fetch it from the map:

var fieldValue = users['foo'];