2017-10-08 66 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)