【问题标题】:Getting null values calling method in initState() in flutter在flutter中获取initState()中的空值调用方法
【发布时间】:2020-02-05 18:38:27
【问题描述】:
   // I called widget from widget
   Navigator.of(context).pushNamed('/placebids', arguments: {
  'ProductId': productDocumentId,
  'ProductName': productName,

});


String productId, productName ;

  @override
 void initState() {
   super.initState();
   _fetchBidofAllTheUsersForCompare();
  }

 Future<void> _fetchBidofAllTheUsersForCompare() async {
 final QuerySnapshot _dbr = 
    await Firestore.instance.collection("Pro-$productName-$productId").getDocuments();
    List<DocumentSnapshot> templist;
  templist = _dbr.documents;
  List<Map<dynamic, dynamic>> list = new List();
  list = templist.map((DocumentSnapshot ds) {
      return ds.data;
  }).toList();
    list.forEach((item) => {
      item.forEach((key, value) => {
        print(value)

  })
});

}

Widget build(BuildContext context) {
final routeArguments =
    ModalRoute.of(context).settings.arguments as Map<String, String>;
productId = routeArguments['ProductId'];
productName = routeArguments['ProductName'];
return Scaffold();
}

当我尝试调用 initState() 中的方法时获取 ProductId 和 ProductName 的 null 值;如果我在 Widget 中收到这些值,那么如何在 initState 方法中使用它

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    我遇到过类似的空值问题,使用DocumentSnapshot时的空值问题

    final DocumentSnapshot_dbr = 
        await Firestore.instance.collection("Pro-$productName-$productId").getDocuments();
    

    仅限documentsnapshot_dbr

    【讨论】:

    • 另外,当您使用 Firestore 操作时,请小心使用“等待”,以防将来操作
    猜你喜欢
    • 2018-09-02
    • 2020-10-08
    • 2020-07-15
    • 2021-11-14
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    相关资源
    最近更新 更多