我有来自restapi和wnat的响应来访问注释中的回复,我有来自quicktype.io的模型,但不知道如何迭代和获取数据。
"data": {
"_id": "61ee65fd92a48c7b10779f24",
"topicName": "Test Topic",
"description": "<p>Test Discussion</p>",
"createdByName": "Admin",
"comments": [
{
"_id": "622740a70fdfbb1bdd00232b",
"comment": "post commnet test",
"commentedById": "611cb5ecaf1a1bbc858d4d13",
"user": [
{
"title": "Mr",
"firstName": "John",
"lastName": "Cart",
"userID": "USER1000022",
"_id": "611cb5ecaf1a1bbc858d4d13"
}
],
"replies": []
},
{
"_id": "620d05382773643acd49177b",
"comment": "test message",
"commentedById": "611cb5ecaf1a1bbc858d4d13",
"commentedByName": "Mr. John Cart",
"user": [
{
"title": "Mr",
"firstName": "John",
"lastName": "Cart",
"userID": "USER1000022",
"_id": "611cb5ecaf1a1bbc858d4d13"
}
],
"replies": [
{
"_id": "6227351b0fdfbb099800232a",
"comment": "test msg",
"commentedById": "611cb5ecaf1a1bbc858d4d13",
"commentedByName": "Mr, john cart",
"replyUser": [
{
"title": "Mr",
"firstName": "John",
"lastName": "cart",
"userID": "USER1000022",
"_id": "611cb5ecaf1a1bbc858d4d13"
}
]
}
],
},任何帮助或文件/建议/指南都是有帮助的
谢谢
发布于 2022-03-09 06:16:54
var object= json.decode(response.body)对象变量必须采用重复语法。
print(object["data"]["comments"][index]["replies"]发布于 2022-03-09 06:42:08
从internet获取数据
此菜谱使用以下步骤:
dependencies: http: <latest_version>
导入http包
import 'package:http/http.dart' as http;此外,在您的AndroidManifest.xml文件中,添加Internet权限。
<!-- Required to fetch data from the internet. -->
<uses-permission android:name="android.permission.INTERNET" />发出网络请求
Future<YourModel> fetchData() async {
final response = await http
.get(Uri.parse('yourapiurl'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return YourModel.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load data');
}
}获取数据
class _MyAppState extends State<MyApp> {
late Future<YourModel> yourAlbum;
@override
void initState() {
super.initState();
yourModel = fetchData();
}
// ···
}显示数据
FutureBuilder<YourModel>(
future: futureModel,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!.title);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
)https://stackoverflow.com/questions/71405009
复制相似问题