androiddartflutter

Widget not returning view on Future<bool> condition in flutter


Widget not returning view. isAttrAssignedToProduct in this method I'm trying to find the assigned attribute in the table if the product exists in the table then return true and the image will be inflated in listview item on the base of true or false. My database returning true or false on condition but the view is not visible.

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');
  }

My Query working fine

 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;
    }
  }

My List Tile

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
        ),

Solution

  • your function Widget isAttrAssigned is not returning anything.

    If you want to build a widget based on a Future, you can use a FutureBuilder.

    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 is your boolean

    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); 
    }