List.generate 不返回任何值 Flutter Sqflite

时间:2021-04-16 12:49:55

标签: flutter

我想使用 final List> maps = await db.query('product');

这是我的完整功能代码。

    Future<List<ProductModel>> getProducts()async{
    final Database db = await dataBaseHelper.connect();
    // Query the table for all The Users.
    final List<Map<String, dynamic>> maps = await db.query('product');
    db.close();

    List<ProductModel> result = List.generate(maps.length, (i) {
      return ProductModel(
          productID: maps[i]['id'],
          adminID: maps[i]['admin_id'],
          name: maps[i]['product_name'],
          soldType: maps[i]['sold_type'],
          category: maps[i]['product_category'],
          price: maps[i]['price'],
          stock: maps[i]['stock'],
          sold: maps[i]['sold'],
          sales: maps[i]['sales'],
          unit: maps[i]['unit']
      );
    });
    print(result);
    return result;
  }

然后在我展示产品的管理端

我使用 Futurebuilder

这是代码

return FutureBuilder<List<ProductModel>>(
           future: productManager.getProducts(),
           builder: (BuildContext context,snapshot){
             if(!snapshot.hasData)return Center(child: CircularProgressIndicator(),);
             products = snapshot.data;
             print(productManager.getProducts());
             return ListView(
               children: products.map((product) {
                 return Slidable(
                   actionPane: SlidableScrollActionPane(),
                   actions: [
                     IconSlideAction(
                       caption: "Update",
                       color: Colors.white.withAlpha(0),
                       icon:Icons.update,
                       onTap: (){
                         openAddProductDialog(
                           confirmText: "Update Product",
                           context:context,
                           title:Container(
                               decoration: BoxDecoration(
                                   borderRadius: BorderRadius.only(topLeft:Radius.circular(20),
                                       topRight: Radius.circular(20) ),
                                   color: defaultBlack
                               ),
                               padding: EdgeInsets.all(20),
                               child:Text("Update Product",style: TextStyle(color: Colors.white,fontFamily: "Lemon"),textAlign: TextAlign.center,),
                           ),
                           product:product
                         );
                       },
                     )
                   ],
                   secondaryActions: [
                     IconSlideAction(
                         caption: "Delete",
                         icon:Icons.delete,
                         color: Colors.white.withAlpha(0),
                         foregroundColor: defaultButtonColor2,
                         onTap: (){

                         },
                     )
                   ],
                   child: Container(
                     alignment: Alignment.bottomLeft,
                       padding: EdgeInsets.all(10),
                       child:Row(
                         mainAxisAlignment: MainAxisAlignment.spaceBetween,
                         children: [
                           Column(
                             crossAxisAlignment: CrossAxisAlignment.start,

                             children: [
                               Text(product.name,style: TextStyle(color:defaultBlack,fontSize: 25,fontFamily: "Lemon"),),
                               Row(

                                 children: [
                                   Text("Stock: "+product.stock.toString()+(product.soldType.contains('byweight')?"/"+product.unit:"pieces"),style: TextStyle(color:defaultBlack,fontSize: 13,fontWeight: FontWeight.w300),),
                                   Padding(padding: EdgeInsets.only(left:20)),
                                   Text("Sold: "+product.sold.toString()+(product.soldType.contains('byweight')?"/"+product.unit:"pieces"),style: TextStyle(color:defaultBlack,fontSize: 13,fontWeight: FontWeight.w300),),
                                 ],
                               ),
                             ],

                           ),
                           Column(
                             crossAxisAlignment: CrossAxisAlignment.end,
                             children: [
                               Text(product.price.toString(),style: TextStyle(color:defaultOrange,fontSize: 25,fontFamily: "Lemon")),
                               Text((product.soldType.contains('byweight')?"/"+product.unit:"pieces"),style: TextStyle(color:defaultBlack,fontSize: 15,fontWeight: FontWeight.bold),),
                             ],
                           ),
                           // Column(
                           //   crossAxisAlignment: CrossAxisAlignment.end,
                           //   children: [
                           //     Text(product.price.toString(),style: TextStyle(color:defaultOrange,fontSize: 25,fontFamily: "Lemon")),
                           //     Text((product.isWeighable?"/"+product.unit:"pieces"),style: TextStyle(color:defaultBlack,fontSize: 15,fontWeight: FontWeight.bold),),
                           //   ],
                           // )
                         ],
                       )
                   ),
                 );
               }).toList(),
             );
           }
       );

在 getProducts() 上,我尝试打印结果但它什么也没显示, 这就是为什么在我尝试显示所有产品的管理端,getProdcuts() 函数总是返回空的 :( 也许我错过了什么?。

我对用户使用相同的技术并且它有效,但在这方面没有任何作用。

0 个答案:

没有答案