从列表<Object>创建下拉列表



首先我有这些类来从api 获取json数据

import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:wnetworking/wnetworking.dart';
class Offices {
final String name, value;
/* ---------------------------------------------------------------------------- */
Offices(this.name, this.value);
/* ---------------------------------------------------------------------------- */
@override
String toString() => '$name [$value]';
}
class DeltaPrima {
DeltaPrima._();
/* ---------------------------------------------------------------------------- */
static const _url = 'http://deltaprima.rmdoo.com/api/office/all';
/* ---------------------------------------------------------------------------- */
static Future<List<Offices>?> getOffices(String token) async {
var result =
(await HttpReqService.get<List>(_url, headers: {'CompanyCode': token}));
if (result == null) return null;
var list = result
.cast<JMap>()
.map((m) => Offices(m['CODD_DESC']!, m['CODD_VALU']!))
.toList();
print('Objects => $list');
return list;
}
}

然后为了运行它,我有这样的getCust()方法。

Future<Map<String, dynamic>?> getCust() async {
Uri url = Uri.parse("http://deltaprima.rmdoo.com/api/business");
var response = await http.get(url);
if (response.statusCode != 200) {
print("error");
return null;
} else {
setState(() {
data = jsonDecode(response.body) as Map<String, dynamic>;
offices = DeltaPrima.getOffices(data["CODD_VALU"]);
});
}
}

在我运行这些代码后,我得到了这个返回

Objects => [DELTA PRIMA [01], SAMPLE [02]]

我希望这些DELTA PRIMASAMPLE成为下拉窗口小部件中的项目。那么我想要0102作为它们的值。

我该怎么做?

Office创建Dropdown类似的模型

DropdownButton<Offices>(
items: offices.map((Offices ofc) {
return DropdownMenuItem<Offices>(
value: ofc,
child: Text(ofc.name),
);
}).toList(),
onChanged: (_) {},
)

最新更新