Flutter 使用FutureBuilder获取JSON数据的Flatter下拉按钮
我试图添加一个字符串列表到一个来自未来的DropdwonButton,但它不能正常工作,我是一个新的颤振 我编写了一个FutureBuilder来从API(各国的Lits)获取数据,它工作得很好,但是当我将数据添加到DropdOnButton时,它什么也不显示,并且指示的错误是“MethodAdd调用null” 这是我的密码: 声明:Flutter 使用FutureBuilder获取JSON数据的Flatter下拉按钮,flutter,flutter-futurebuilder,Flutter,Flutter Futurebuilder,我试图添加一个字符串列表到一个来自未来的DropdwonButton,但它不能正常工作,我是一个新的颤振 我编写了一个FutureBuilder来从API(各国的Lits)获取数据,它工作得很好,但是当我将数据添加到DropdOnButton时,它什么也不显示,并且指示的错误是“MethodAdd调用null” 这是我的密码: 声明: List countries = List(); Positions pos; Future<Positions> _posi; FutureBui
List countries = List();
Positions pos;
Future<Positions> _posi;
FutureBuilder:
FutureBuilder(
future: _posi,
builder: (_, AsyncSnapshot<Positions> snapshot){
switch (snapshot.connectionState) {
case ConnectionState.active:
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
break;
case ConnectionState.done:
if (snapshot.hasData && !snapshot.hasError) {
if (snapshot.data == null) {
return Text("No Data",style: new TextStyle( fontSize: 20.0));
} else {
for(int i=0; i<= snapshot.data.countries.length;i++){
countries.add(snapshot.data.countries[i].name);
}
return
Padding(
padding: EdgeInsets.only(top: size.height * 00.2383, bottom: 1),
child: ListView(
padding: const EdgeInsets.all(10),
children: <Widget>[
Container(
height: size.height * 0.1047,
decoration: BoxDecoration(
color:NeumorphicTheme.baseColor(context),
borderRadius: BorderRadius.circular(29)),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
new DropdownButton(
value: currentCounty,
items: _dropDownMenuItemsCountries,
onChanged: changedDropDownItemCountry,
),SizedBox(
width: size.width * .55,
),
Image.asset('assets/images/r3.png'),
],
)),
SizedBox(height: size.height * 0.0369),
FutureBuilder(
未来:_posi,
生成器:(\ux,异步快照){
交换机(快照.连接状态){
案例连接状态.active:
案例连接状态。正在等待:
返回中心(子项:CircularProgressIndicator());
打破
案例连接状态。完成:
if(snapshot.hasData&!snapshot.hasError){
如果(snapshot.data==null){
返回文本(“无数据”,样式:newtextstyle(fontSize:20.0));
}否则{
对于(inti=0;i,您需要将项目声明为小部件列表,例如
项目:[“一”、“二”、“免费”、“四”]
.map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
}).toList(),
这将创建一个列表[文本('One'),…]您是否调试了此项,是否为快照添加了监视?我假设snapshot.data.countries.不正确。因此,请查看作为快照传递的内容。是的,当我打印国家列表时,它会显示从API抓取的国家。@w461在哪一行抛出错误?是否_dropdownMenuItemsEntries一个co列表每个国家/地区的国家/地区或文本窗口小部件的数量?\u dropDownMenuItemsCountries是一个项目列表。下面是我传递的方法:list getDropDownMenuItemsCountries(){list itemscountry=new list();for(国家/地区中的字符串国家/地区){itemscountry.add(new DropdownMenuItems)(值:country,child:new Text(国家));}返回项目中心;}
@override
void initState() {
super.initState();
_posi = getPosition();
_dropDownMenuItemsCountries = getDropDownMenuItemsCountries();
currentCounty = _dropDownMenuItemsCountries[0].value;
}
FutureBuilder(
future: _posi,
builder: (_, AsyncSnapshot<Positions> snapshot){
switch (snapshot.connectionState) {
case ConnectionState.active:
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
break;
case ConnectionState.done:
if (snapshot.hasData && !snapshot.hasError) {
if (snapshot.data == null) {
return Text("No Data",style: new TextStyle( fontSize: 20.0));
} else {
for(int i=0; i<= snapshot.data.countries.length;i++){
countries.add(snapshot.data.countries[i].name);
}
return
Padding(
padding: EdgeInsets.only(top: size.height * 00.2383, bottom: 1),
child: ListView(
padding: const EdgeInsets.all(10),
children: <Widget>[
Container(
height: size.height * 0.1047,
decoration: BoxDecoration(
color:NeumorphicTheme.baseColor(context),
borderRadius: BorderRadius.circular(29)),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
new DropdownButton(
value: currentCounty,
items: _dropDownMenuItemsCountries,
onChanged: changedDropDownItemCountry,
),SizedBox(
width: size.width * .55,
),
Image.asset('assets/images/r3.png'),
],
)),
SizedBox(height: size.height * 0.0369),