【发布时间】:2021-09-22 00:34:18
【问题描述】:
我正在尝试在 Flutter 中呈现元素列表,并且列表数据来自 REST API。
我似乎找不到可以让我提取参数的示例
frm_payload
来自以下 JSON 输出。消息再次出现,我希望得到一个显示该字段值的字符串列表,在以下示例中为:
T1BF
{
"result": {
"end_device_ids": {
"device_id": "unitx1",
"application_ids": {
"application_id": "jandraapp"
},
"dev_eui": "A8A55920F540DBDF",
"dev_addr": "260BC823"
},
"received_at": "2021-07-12T18:28:33.504479658Z",
"uplink_message": {
"f_port": 1,
"f_cnt": 5,
"frm_payload": "T1BF",
"rx_metadata": [
{
"gateway_ids": {
"gateway_id": "packetbroker"
},
"packet_broker": {
"message_id": "01FADZMPC8E58VRNYC5WWQRRFS",
"forwarder_net_id": "000013",
"forwarder_tenant_id": "ttnv2",
"forwarder_cluster_id": "ttn-v2-eu-1",
"forwarder_gateway_eui": "58A0CBFFFE803615",
"forwarder_gateway_id": "eui-58a0cbfffe803615",
"home_network_net_id": "000013",
"home_network_tenant_id": "ttn",
"home_network_cluster_id": "ttn-eu1"
},
"time": "2021-07-12T18:28:33.277776956Z",
"rssi": -90,
"channel_rssi": -90,
"snr": 7.75
}
],
"settings": {
"data_rate": {
"lora": {
"bandwidth": 125000,
"spreading_factor": 7
}
},
"data_rate_index": 5,
"coding_rate": "4/5",
"frequency": "867500000"
},
"received_at": "2021-07-12T18:28:33.295783006Z",
"consumed_airtime": "0.051456s"
}
}
}
这是我从我的飞镖代码中调用它的方式,在哪里
发帖
是解码后的 JSON 对象
"frm_payload: ${post["result"]["uplink_message"]["frm_payload"]}"
这是我在颤振中的完整代码:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Index 0: Homexx',
style: optionStyle,
),
Text(
'Index 1: Business',
style: optionStyle,
),
Text(
'Index 2: School',
style: optionStyle,
),
Text(
'Index 3: Settings',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
final url =
"https://eu1.cloud.thethings.network/api/v3/as/applications/jandraapp/devices/unitx1/packages/storage/uplink_message";
// final url = "https://jsonplaceholder.typicode.com/posts";
var _postsJson = [];
void fetchPosts() async {
print('FETCHPOSTS_PRETRY');
try {
final response = await get(Uri.parse(url));
final jsonData = jsonDecode(response.body) as List;
setState(() {
_postsJson = jsonData;
print('FETCHPOSTS');
});
} catch (err) {
print(' FORMATTING ERROR');
}
}
@override
void initState() {
super.initState();
fetchPosts();
}
@override
Widget build(BuildContext context) {
print(_postsJson.length);
return Scaffold(
appBar: AppBar(
title: const Text('Device Status Screen'),
),
body: ListView.builder(
itemCount: _postsJson.length,
itemBuilder: (context, i) {
final post = _postsJson[i];
return Text(
"frm_payload: ${post["result"]["uplink_message"]["frm_payload"]}"); // ***** THIS IS THE PROBLEM CODE*******
// return Text("Title: ${post["title"]}\n Body: ${post["body"]}\n\n");
},
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
backgroundColor: Colors.red,
),
BottomNavigationBarItem(
icon: Icon(Icons.history),
label: 'History',
backgroundColor: Colors.green,
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
backgroundColor: Colors.pink,
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
【问题讨论】: