jsonapi的flutter下拉列表 [英] flutter dropdown from jsonapi
本文介绍了jsonapi的flutter下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码运行正常,并在列表视图中显示数据:
This code is working perfectly fine and showing data in listview:
return ListView.builder(
itemCount: snapshot.data.data.length,
itemBuilder: (context, index) {
print(snapshot.data.data[index]);
return ListTile(
title:
Text(snapshot.data.data[index].id.toString()),
subtitle: Text(
snapshot.data.data[index].attributes.name==null?'Not Set':snapshot.data.data[index].attributes.name,
),
);
});
但是当我在下拉窗口小部件中使用它时,它会抛出错误:
but when I am using the same in dropdown widget, it is throwing error:
return DropdownButton<String>(
hint: Text("Select"),
value: _selectedTestkit,
onChanged: (newValue) {
setState(() {
_selectedTestkit = newValue;
});
},
items: snapshot.data.data.map((item) =>
DropdownMenuItem<String>(
child: Text(item.id),
value:item.id,
)).toList(),
);
}
}
}),
),
这是我得到的错误:
There should be exactly one item with [DropdownButton]'s value: .
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 855 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
但是在列表视图生成器中,我得到以下结果:
but in listview builder I am getting this result:
1 testname1
2 testname2
3 testname3
4 testname4
更新:
我曾经在下拉列表中添加自定义值,如下所示:
I used to add a custom value in dropdown list like this:
List data = json.decode(response.body);
data.add('other');
,另一个将显示在下拉列表中.
and the other will show up in the dropdownlist.
但是当我尝试使用当前的未来方法时
but when I am trying in the present future method
Future<TestkitList> fetchTestkitId() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
Uri testkiturl = Uri.https(staging_url, "testkits");
final response = await http.get(testkiturl, headers: {
'Accept-Type': 'application/json',
"Content-Type": "application/json",
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
print(response.body);
print('this was printed before json decode');
var responsetestkit = Future.value(testkitListFromMap(response.body));
responsetestkit.add('other');//here I am trying to add a custom item for dropdown
setState(() {
_testkit = responsetestkit;
print(_testkit);
});
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
print(response.body);
throw Exception('Failed to load TESTKITLIST');
}
}
但是在这里我得到了一个错误-未为类型'Future'定义方法'add'.
but here I am getting the error - The method 'add' isn't defined for the type 'Future'.
推荐答案
更改此内容
if (response.statusCode == 200) {
return Future.value(testkitListFromMap(response.body));
} else {
throw Exception('Failed to load TESTKITLIST');
}
对此:
if (response.statusCode == 200) {
return Future.value(testkitListFromMap(response.body["data"]));//you need to add "data" to access your items based on your data class.
} else {
throw Exception('Failed to load TESTKITLIST');
}
这篇关于jsonapi的flutter下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文