skip to Main Content

This is My Code.

Future<void> SendOrderDetails() async{
  Row(
    children: [
      FutureBuilder(
      future: topcart.getData(),
        builder: (context, AsyncSnapshot<List<Cart>> snapshot) {
          for(int i = 0; i<itemcount; i++)
          {
            if(itemcount>listModel2.data!.length) {
              listModel2.data?.add(Model2(
                ORDER_INFO_ID: 1,
                PRODUCT_ED_ID: 2,
                QTY: quantitycalcule.toString(),
                UNIT_PRICE:'00',// snapshot.data![i].Book_initional_price!.toString(),
                CHGED_BY:1,
                CHGED_DATE: DateTime.now().toString(),
                STATUS: 'P',
              ),);
            }
          }
          return const Text('');
        }
       ),
    ],
   );
}

When I Call This, "FutureBuilder" did not run. I need "snapshot" in If condition. Please Help me.

3

Answers


  1. this method returns nothing so you don’t have way to check whether the future builder run or not, try to use print and check your debug console (or try to debug your code by using break points) if it works then do what ever you want to do
    additional question: what state management you are using?

    Login or Signup to reply.
  2. I’m not sure what your code is trying to accomplish but there are a few things I can see that could be potentially causing issues:

    1. You are calling FutureBuilder inside a Future and there is no await inside your future so it’s really not going to work right.

    The whole point of a FutureBuilder is to builds itself based on the latest snapshot of interaction with a Future. Maybe you can explain why this structure is the way it is.

    1. topcart.getData() – Should not be in the future builder. You need to get something like
    // This needs to be outside the build method, maybe initState()
    Future<TYPE> _gd = topcart.get() 
    
    // Future Builder in the build method
    FutureBuilder<TYPE>(
            future: _gd, 
            builder: (BuildContext context, AsyncSnapshot<TYPE> snapshot) {});
    
    1. Ideally, within the FutureBuilder() you want to check connection and data like:
     if (snapshot.connectionState == ConnectionState.waiting) {
          return // Progress indicator widget
        } else if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasError) {
            return // Error Widget or Text 
          } else if (snapshot.hasData) {
            return // Process data here
          } else {
            return // Empty set returned
          }
        } else {
          return Text('State: ${snapshot.connectionState}');
        }
    
    Login or Signup to reply.
  3. The whole point of FutureBuilder is to build (and so to return) a Widget based on a snapshot.
    As it seems you don’t need this Widget at all in your code, couldn’t you just skip the Builder altogether ?
    Something like this :

    Future<void> SendOrderDetails() async {
      var data = await topcart.getData();
      for (int i = 0; i < itemcount; i++) {
        if (itemcount > listModel2.data!.length) {
          listModel2.data?.add(
            Model2(
              ORDER_INFO_ID: 1,
              PRODUCT_ED_ID: 2,
              QTY: quantitycalcule.toString(),
              UNIT_PRICE: data![i].Book_initional_price!.toString(),
              CHGED_BY: 1,
              CHGED_DATE: DateTime.now().toString(),
              STATUS: 'P',
            ),
          );
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search