0

I've this code and I don't know how to pass my parameters to the futurbuilder , could someone help me ?

My goal is to pass my parameters from the uri to the futurebuilder

class EstablishmentCategoryProductsPricesWidget extends StatefulWidget {
  const EstablishmentCategoryProductsPricesWidget(String category, String establishment,  {Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => EstablishmentCategoryProductsPricesWidgetState();
}

class EstablishmentCategoryProductsPricesWidgetState extends State<EstablishmentCategoryProductsPricesWidget> {
  @override
  Widget build(BuildContext context) {
    debugPrint("--- BUID ESTABLISHMENT CATEGORY PRODUCTS PRICES ---");
    return Scaffold(
      body: Center(
        child: FutureBuilder<List<EstablishmentCategoryProductsPrices>>(
          future: fetchListEstablishmentCategoryProductsPrices(/*what to write here */),
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Text('Web API call error : ${snapshot.error}');
            }
            else if (snapshot.hasData) {
              return EstablishmentCategoryProductsPricesList(establishmentcategoryproductsprices: snapshot.data!);
            }
            else {
              return const Center(child: CircularProgressIndicator());
            }
          },
        ),
      ),
    );
  }
}

this is the code with the uri

Future<List<EstablishmentCategoryProductsPrices>> fetchListEstablishmentCategoryProductsPrices(String category, String establishment) async {
  final response = await http
      .get(Uri.parse('https://localhost:7282/api/EstablishmentCategoryProductsPrices?EstbalishmentName=$establishment&CategoryName=$category'));
  debugPrint(response.statusCode.toString());
  debugPrint(response.body);
  if (response.statusCode == 200) {
    return compute(parseEstablishmentCategoryProductsPrices, response.body);
  } else {
    throw Exception('Failed to load EstablishmentCategoryProductsPrices');
  }
}

List<EstablishmentCategoryProductsPrices> parseEstablishmentCategoryProductsPrices(String responseBody) {
  final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
  return parsed
      .map<EstablishmentCategoryProductsPrices>((json) => EstablishmentCategoryProductsPrices.fromJson(json))
      .toList();
}
3
  • Can you tell me where that uri is currently in this code? Feb 15, 2022 at 19:42
  • I just add it, sorry
    – GIBS
    Feb 15, 2022 at 20:00
  • Did you mean you're having issues passing values on fetchListEstablishmentCategoryProductsPrices(String, String)? What prevents you from doing so? Is there any errors thrown? It'll be helpful for others if you can be specific with the issue.
    – Omatt
    Feb 16, 2022 at 6:27

1 Answer 1

1

In Flutter (and Dart), you can pass variables in several ways. One of them is passing by the constructor. You already have the variables that you need.

From the state object, you will have a reference to the widget and this way, you can pass those variables while calling your function.

class EstablishmentCategoryProductsPricesWidget extends StatefulWidget {
  const EstablishmentCategoryProductsPricesWidget(this.category, this.establishment,  {Key? key}) : super(key: key);

  final String category;
  final String establishment;

  @override
  State<StatefulWidget> createState() => EstablishmentCategoryProductsPricesWidgetState();
}

class EstablishmentCategoryProductsPricesWidgetState extends State<EstablishmentCategoryProductsPricesWidget> {
  @override
  Widget build(BuildContext context) {
    debugPrint("--- BUID ESTABLISHMENT CATEGORY PRODUCTS PRICES ---");
    return Scaffold(
      body: Center(
        child: FutureBuilder<List<EstablishmentCategoryProductsPrices>>(
          future: fetchListEstablishmentCategoryProductsPrices(widget.category, widget.establishment),
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Text('Web API call error : ${snapshot.error}');
            }
            else if (snapshot.hasData) {
              return EstablishmentCategoryProductsPricesList(establishmentcategoryproductsprices: snapshot.data!);
            }
            else {
              return const Center(child: CircularProgressIndicator());
            }
          },
        ),
      ),
    );
  }
}
1
  • thank you so much, I didn't know this way !!!
    – GIBS
    Feb 16, 2022 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.