2017-10-08 111 views
0

我有一个简单的应用程序与产品列表。产品存储在Firebase Firestore上。我想下载产品清单并给用户更新一些数据的可能性。Flutter Firestore - 查找`文档快照“ID

所以我的产品准备清单:

Widget _buildProductsList() { 
    return new StreamBuilder(
    stream: Firestore.instance 
     .collection('products') 
     .snapshots, 
    builder: (context, snapshot) { 
     if (!snapshot.hasData) return new Text("Loading..."); 

     return new ListView(
     children: snapshot.data.documents.map((document) { 
      Product product = new Product(document.data); 
      _ProductCell cell = new _ProductCell(); 
      cell.product = product; 
      return cell; 
     }).toList(), 
    ); 
    }, 
); 
} 

我序列文件快照到我的产品对象:现在

class Product { 

    static const NAME_KEY = 'name'; 
    static const DESC_KEY = 'desc'; 
    static const PHOTO_URL_KEY = 'photoUrl'; 
    static const AUTHOR_ID_KEY = 'authorId'; 
    static const CREATED_AT_KEY = 'createdAt'; 

    String name; 
    String description; 
    String photoUrl; 
    String authorId; 
    int createdAt; 

    Product(Map<String, dynamic> json) { 
     name = json[NAME_KEY]; 
     description = json[DESC_KEY]; 
     photoUrl = json[PHOTO_URL_KEY]; 
     authorId = json[AUTHOR_ID_KEY]; 
     createdAt = json[createdAt]; 
    } 
} 

,当用户提出一些与ProductCell交互我想更新产品文档中与Firestore链接,但我没有ID,因此无法创建适当的Firestore引用。

如何实现这一目标?

回答

1

由于问题#12471解决了,你可以从文档对象获取文档ID。

print(document.documentID)