【问题标题】:How do i receive a future value as String我如何接收未来值作为字符串
【发布时间】:2019-05-10 00:13:23
【问题描述】:

我正在尝试将未来的返回值作为字符串接收。我该怎么办。

//Get a stock info
Future<String> getStock(int productID) async{
  var dbClient = await db;
  var result = await dbClient.rawQuery('SELECT * FROM $tableStock WHERE $columnProductID = $productID');
  if(result.length == 0) return null;
  return Stock.fromMap(result.first).currentStock;
}


Widget _buildProductInfo(Product data){
    return Container(
      child: ListView(
        padding: EdgeInsets.all(8.0),
        children: <Widget>[
           _infoRow('Product ID', data.name),
           _infoRow('Product Name', data.productID),
           _infoRow('Cost Price', data.costPrice),
           _infoRow('Selling Price', data.salePrice),
           _infoRow('CategoryID', data.categoryID),
           _infoRow('Currrent Stock', db.getStock(int.parse(data.productID)))
        ],
      ),
    );
  }

我希望这段代码显示一个“值”而不是“未来的实例”。但是我可以在尝试时打印返回值

final res = await db.getStock(int.parse(data.productID);
print(res);

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您必须等待未来才能解开价值。您可以使用未来的构建器来执行此操作。

    而不是这个:

    _infoRow('Currrent Stock', db.getStock(int.parse(data.productID))),
    

    有这个:

    FutureBuilder(
        future: db.getStock(int.parse(data.productID),
        builder: (context, snapshot) => _infoRow('Currrent Stock', snapshot.data),
    ),
    

    您的完整代码将如下所示:

    child: StreamBuilder<Product>(
           initialData: barcode,
           stream: bloc.scannedCode,
           builder: (BuildContext context, AsyncSnapshot snapshot){
             if (snapshot.hasError) return Text('Error: ${snapshot.error}');
            switch (snapshot.connectionState) {
              case ConnectionState.none:
                return Text('Select lot');
              case ConnectionState.waiting:
                return _buildProductInfo(snapshot.data);
              case ConnectionState.active:
              case ConnectionState.done:
                return _buildProductInfo(snapshot.data);
            }
           },
         )
    
    Widget _buildProductInfo(Product data){
        return Container(
          child: ListView(
            padding: EdgeInsets.all(8.0),
            children: <Widget>[
               _infoRow('Product ID', data.name),
               _infoRow('Product Name', data.productID),
               _infoRow('Cost Price', data.costPrice),
               _infoRow('Selling Price', data.salePrice),
               _infoRow('CategoryID', data.categoryID),
               FutureBuilder(
                   future: db.getStock(int.parse(data.productID),
                   builder: (context, snapshot) => _infoRow('Currrent Stock', snapshot.data),
               )
            ],
          ),
        );
      }
    

    【讨论】:

    • 我不能使用异步,因为 _buildProductInfo() 预计会返回 Widget 而不是未来。当我这样做时,我得到了这个错误“类型'Future'不是'Widget'类型的子类型”。我已经添加了调用函数的位置。
    • @David 您似乎对两个答案都留下了相同的评论,但是您尝试了我的建议吗?
    • 是的,我做到了。他们都以类似的方式解决问题,因此他们给出了相同的错误。但是我已经能够重组代码来解决问题。我创建了另一个收听股票的流构建器
    【解决方案2】:

    您必须在 _buildProductInfo() 方法上使用 async 并在 db.getStock(int.parse(data.productID)) 之前使用 await 。这样,执行就会暂停,直到 Future 完成。

    【讨论】:

    • 我不能使用异步,因为 _buildProductInfo() 预计会返回 Widget 而不是未来。当我这样做时,我得到了这个错误“类型'Future'不是'Widget'类型的子类型”。我已经添加了调用函数的位置。
    猜你喜欢
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多