Flutter URI 参数 Futurebuilder
Flutter URI Parameters Futurebuilder
我有这段代码,但我不知道如何将我的参数传递给 futurbuilder,有人可以帮我吗?
我的目标是将我的参数从 uri 传递给 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());
}
},
),
),
);
}
}
这是带有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();
}
在 Flutter(和 Dart)中,您可以通过多种方式传递变量。其中之一是通过构造函数。您已经拥有所需的变量。
从状态对象中,您将拥有对小部件的引用,这样,您可以在调用函数时传递这些变量。
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());
}
},
),
),
);
}
}
我有这段代码,但我不知道如何将我的参数传递给 futurbuilder,有人可以帮我吗?
我的目标是将我的参数从 uri 传递给 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());
}
},
),
),
);
}
}
这是带有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();
}
在 Flutter(和 Dart)中,您可以通过多种方式传递变量。其中之一是通过构造函数。您已经拥有所需的变量。
从状态对象中,您将拥有对小部件的引用,这样,您可以在调用函数时传递这些变量。
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());
}
},
),
),
);
}
}