Flutter 如何在 http.post 上使用 DropDowns 选中的项目
Flutter How to use DropDowns selected item on http.post
我有一个下拉按钮,其中包含从 api 获取的项目列表,我需要将所选项目的 ID 发送到服务器。因此,当用户选择项目并按下“开始工作”按钮时,我需要将所选项目 ID 发送到服务器,我遇到的问题是发送所选项目 ID。
Future<void> postLocationId(String id) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? authorization = prefs.getString('authorization');
var url = 'some url';
try {
final response = await http.post(
Uri.parse(url),
headers: <String, String>{
'authorization': authorization ?? basicAuth.toString(),
"Content-Type": "application/json"
},
body: jsonEncode(id)
);
print(response.statusCode);
print(id);
} catch (er) {}
}
String? chooseLocation;
late Future<Response> futureData;
late Future<Response> futureDataForAccount;
bool _flag = true;
List<WorkingLocationData>? workingData;
List<AccountData>? accountData;
Scaffold(
body: FutureBuilder<List<Response>>(
future: Future.wait([futureData, futureDataForAccount]),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<WorkingLocationData> data1 =
parsedData(snapshot.data![0].body);
workingData = data1;
AccountData data3 =
AccountData.fromJson(json.decode(snapshot.data![1].body));
child: DropdownButton<String>(
value: chooseLocation,
hint: const Text('Select a location'),
isExpanded: true,
items: workingData!.map((some) {
return DropdownMenuItem(
child: Text(some.name + ' (${some.location})'),
value: some.id,
);
}).toList(),
onChanged: (String? displayedValue) {
setState(
() {
chooseLocation = displayedValue!;
},
);
},
),
),
SizedBox(height: height * 0.150),
Column(
children: [
ElevatedButton(
onPressed: () async {
final WorkingLocationData locatonData = workingData!.firstWhere((some) => some.location == chooseLocation);
await postLocationId(locatonData.id);
print(locatonData.id);
setState(() {
_flag = !_flag;
});
},
child: Text(_flag ? 'Start Work' : 'Stop Work'),
style: ElevatedButton.styleFrom(
primary: _flag ? Colors.teal : Colors.red,
),
),
],
您需要添加您想要的参数:postLocationId(String id)
.
在不过多改变结构的情况下,您可以通过 workingData 搜索具有匹配位置的项目并将其传递给按下:
ElevatedButton(
onPressed: () async {
final WorkingLocationData locatonData = workingData!.firstWhere((some) => some.location == chooseLocation);
postLocationId(locatonData.id);
setState(() {
_flag = !_flag;
});
},
child: Text(_flag ? 'Start Work' : 'Stop Work'),
style: ElevatedButton.styleFrom(
primary: _flag ? Colors.teal : Colors.red,
),
),
如果您对更大的结构变化感兴趣,我将 https://pub.dev/packages/flutter_typeahead 用于此类事情
我有一个下拉按钮,其中包含从 api 获取的项目列表,我需要将所选项目的 ID 发送到服务器。因此,当用户选择项目并按下“开始工作”按钮时,我需要将所选项目 ID 发送到服务器,我遇到的问题是发送所选项目 ID。
Future<void> postLocationId(String id) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? authorization = prefs.getString('authorization');
var url = 'some url';
try {
final response = await http.post(
Uri.parse(url),
headers: <String, String>{
'authorization': authorization ?? basicAuth.toString(),
"Content-Type": "application/json"
},
body: jsonEncode(id)
);
print(response.statusCode);
print(id);
} catch (er) {}
}
String? chooseLocation;
late Future<Response> futureData;
late Future<Response> futureDataForAccount;
bool _flag = true;
List<WorkingLocationData>? workingData;
List<AccountData>? accountData;
Scaffold(
body: FutureBuilder<List<Response>>(
future: Future.wait([futureData, futureDataForAccount]),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<WorkingLocationData> data1 =
parsedData(snapshot.data![0].body);
workingData = data1;
AccountData data3 =
AccountData.fromJson(json.decode(snapshot.data![1].body));
child: DropdownButton<String>(
value: chooseLocation,
hint: const Text('Select a location'),
isExpanded: true,
items: workingData!.map((some) {
return DropdownMenuItem(
child: Text(some.name + ' (${some.location})'),
value: some.id,
);
}).toList(),
onChanged: (String? displayedValue) {
setState(
() {
chooseLocation = displayedValue!;
},
);
},
),
),
SizedBox(height: height * 0.150),
Column(
children: [
ElevatedButton(
onPressed: () async {
final WorkingLocationData locatonData = workingData!.firstWhere((some) => some.location == chooseLocation);
await postLocationId(locatonData.id);
print(locatonData.id);
setState(() {
_flag = !_flag;
});
},
child: Text(_flag ? 'Start Work' : 'Stop Work'),
style: ElevatedButton.styleFrom(
primary: _flag ? Colors.teal : Colors.red,
),
),
],
您需要添加您想要的参数:postLocationId(String id)
.
在不过多改变结构的情况下,您可以通过 workingData 搜索具有匹配位置的项目并将其传递给按下:
ElevatedButton(
onPressed: () async {
final WorkingLocationData locatonData = workingData!.firstWhere((some) => some.location == chooseLocation);
postLocationId(locatonData.id);
setState(() {
_flag = !_flag;
});
},
child: Text(_flag ? 'Start Work' : 'Stop Work'),
style: ElevatedButton.styleFrom(
primary: _flag ? Colors.teal : Colors.red,
),
),
如果您对更大的结构变化感兴趣,我将 https://pub.dev/packages/flutter_typeahead 用于此类事情