0

当我必须返回一个字段名时,为什么它会说“没有为类型 'Map<String, dynamic>' 定义 getter '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.
             },
           );

提前致谢。

4

1 回答 1

0

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'];
于 2021-04-03T10:49:20.130 回答