小部件在Flutter中未返回Future <bool>条件下的视图

时间:2019-04-15 13:13:19

标签: android dart flutter

小部件不返回视图。 isAttrAssignedToProduct在这种方法中,如果产品存在于表中,我试图在表中查找分配的属性,然后返回true,并且图像将基于true或false膨胀到listview项目中。我的数据库在条件上返回true或false,但是视图不可见。

Widget isAttrAssigned(int index){
Future result = isAttrAssignedToProduct(index);
return FutureBuilder(
    future: result,
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      result.then((v){
        debugPrint('FUTURE VALE ${v}');
        attr(v);
      });
},);
 Widget attr(bool v){
    return v==true?Container(
      width: 20.0,
      height: 20.0,
      decoration: BoxDecoration(
          color: Colors.yellow),
      child: Image.asset('assets/images/right.png'),
    ):Text('df');
  }

我的查询工作正常

 Future<bool> isAttrAssignedToProduct(int index) async {
    List<Map<String, dynamic>> assd=new List<Map<String, dynamic>>();
    try{
      assd= await dbHelper.queryReadGroupAssignedAttribute(productList[index][DatabaseHelper.columnpid]);
      if(assd.length>0){
       // debugPrint('ASSIGNED ATTR PRESENT ${assd.length}');
        return true;
      }else{
        return false;
      }
    }catch(e){
      //debugPrint('ASSIGNED ATTR EXCEPTION ${e}');
      return false;
    }
  }

我的列表图块

ListTile(
          contentPadding: EdgeInsets.only(left: height*0.03,right: height*0.03 ),
          onTap: (){
            Navigator.push(context,
              MaterialPageRoute(builder: (context) => ProductDetails(pid: productList[index]["pid"],),
                  settings: RouteSettings(name: '/productdetail')),).then((value){
                    setState(() {
                      length=value;
                    });
                    debugPrint('CEHCK BACK FROM DAETAIL $length');
            });
          },
          title: Text(
              '${title}',
              style: TextStyle(fontFamily: 'semibold',color: MyColors.colorPrimaryDark,fontSize: 18.0)),
          subtitle: Text(
              'Price \$${price}',
              style: TextStyle(color: Colors.grey,fontSize: 14.0,fontFamily: 'semibold')),
          trailing:isAttrAssigned( index), //here i'm trying to inflate image
        ),

2 个答案:

答案 0 :(得分:0)

您应该使用FutureBuilder。链接中的示例。很好用

答案 1 :(得分:0)

您的函数Widget isAttrAssigned不返回任何内容。

如果要基于Future构建窗口小部件,则可以使用FutureBuilder https://docs.flutter.io/flutter/widgets/FutureBuilder-class.html

Widget isAttrAssigned(int index) {
   // maybe you want to cache your result here, to avoid doing a call at each rebuild
   Future result = isAttrAssignedToProduct(index);

   return FutureBuilder(
      future: result
      builder: (BuildContext context, AsyncSnapshot snapshot) {
     // return widget
   });

}

snapshot.data是您的布尔值

switch (snapshot.connectionState) { 
case ConnectionState.none: 
return Container(); // empty or show a placeholder 
case ConnectionState.active: 
case ConnectionState.waiting: 
return Text('Loading ...'); 
case ConnectionState.done: 
if (snapshot.hasError) { 
return Text('Error: ${snapshot.error}'); 
} 
return attr(snapshot.data); 
}