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();
}
CodePudding user response:
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());
}
},
),
),
);
}
}