Home > Back-end >  How to call snackbar on function's response in flutter
How to call snackbar on function's response in flutter

Time:08-24

I am building an e-commerce app in a flutter. I want to implement a conditional snack bar. When I get added to cart successfully response then want to show the snack bar with a success message and if not then with the failed message. but I am getting some errors.

 onPressed: () {
                  FutureBuilder(
                    future: ApiServices.addToCart(productId),
                    builder: (BuildContext context, AsyncSnapshot snapshot) {
                      if (snapshot.hasData) {
                         return ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      behavior: SnackBarBehavior.floating,
                      content: Row(
                        children: const [
                          Icon(Icons.shopping_bag),
                          SizedBox(
                            width: 10,
                          ),
                          Text('Product added to my cart !')
                        ],
                      ),
                    ),
                  );
               
                      }
                    },
                  );
                },

** api function **


 static addToCart(int productId) async {
    SharedPreferences prefss = await SharedPreferences.getInstance();
    var token = prefss.getString('token');
    var response = await http.post(
      Uri.parse("$baseURL/adc"),
      body: {
        "product_id": productId.toString(),
      },
      headers: {
        'Authorization': 'Bearer $token',
      },
    );
    if (response.statusCode == 200) {
      return "done";
    } else {
      return "failed";
    }
  }

CodePudding user response:

This code doesn't make sense. In onPressed don't use FutureBuilder at all and do it like

ApiServices.addToCart(productId).then((result) {
     ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      behavior: SnackBarBehavior.floating,
                      content: Row(
                        children: const [
                          Icon(Icons.shopping_bag),
                          SizedBox(
                            width: 10,
                          ),
                          Text('Product added to my cart !')
                        ],
                      ),
                    ),
                  );
               }
             );

CodePudding user response:

static addToCart(BuildContext context,int productId) async {
SharedPreferences prefss = await SharedPreferences.getInstance();
var token = prefss.getString('token');
var response = await http.post(
  Uri.parse("$baseURL/adc"),
  body: {
    "product_id": productId.toString(),
  },
  headers: {
    'Authorization': 'Bearer $token',
  },
);
if (response.statusCode == 200) {
  //Snackbar Code for done
  return "done";
} else {
 //Snackbar Code for fail
  return "failed";
}

}

this will help you FutureBuilder can return only widgets, so you can pass it to call api function

  • Related