【问题标题】:Can I display my JSON data in Table format using DataTable in Flutter?我可以在 Flutter 中使用 DataTable 以表格格式显示我的 JSON 数据吗?
【发布时间】:2021-11-06 08:21:59
【问题描述】:

我在 cosole 中成功获得了 JSON 响应。尝试在颤振中使用DataTable 以表格格式显示它。 我尝试使用这个package,但它只接受 JSON 数组列表格式。但我的 JSON 不在数组列表中。

我的 JSON 响应只是一个 <Map> 我应该使用什么语法在表格中显示它?

{
"EncPartnerId": "LEuT1eIlpLEMAAkZme3wpQ==",
"EncTestId": "U4exk+vfMGrn7cjNUa/PBw==",
"Fee": "100",
"DiscountedFee": "80",
"BookingFee": "50"
}

我的 API 函数:

Future<void> GetTestFee() async {
var jsonResponse;
if (encTestId.isNotEmpty) {
  var response = await http.post(
      Uri.parse("http://medbo.digitalicon.in/api/medboapi/GetTestFee"),
      body: ({
        'EncPartnerId': "LEuT1eIlpLEMAAkZme3wpQ==",
        'EncTestId': "U4exk+vfMGrn7cjNUa/PBw==",

      }));
  if (response.statusCode == 200) {
    print("Correct");
    print(response.body);
    jsonResponse = json.decode(response.body.toString());
    print(jsonResponse);
    //Navigator.push(context,MaterialPageRoute(builder: (context) => DieticianAfterDateSelectPage( rresponse: DieticianEncBookingIdModel.fromJson(jsonResponse),)));
     ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text("Test Added")));
  } else {
    throw Exception("Faild to fetch");
  }
} else {
  throw Exception("Faild to fetch");
}

 }

然后插入 Dummy 数据如何显示我的 JSON 响应数据?我应该使用ListView.builder?

Container(
   child: Column(
    children:[
        DataTable(
              columns: <DataColumn>[
              DataColumn(label: Text("Test Name")),
              DataColumn(label: Text("Fee")),
              DataColumn(label: Text("Discounted Fee")),
              ],

              rows: <DataRow>[
                DataRow(
                   cells: <DataCell>[
                   DataCell(Text("jjj")),
                   DataCell(Text("jsjf")),
                   DataCell(Text("jsjf")),
               ]
             )
          ]
        )
     ]
  )

) 

【问题讨论】:

标签: json flutter api dart


【解决方案1】:

您可以像这样将 json 数据映射到数据表

class User {
String? encPartnerId;
String? encTestId;
String? fee;
String? discountedFee;
String? bookingFee;

User(
  {this.encPartnerId,
  this.encTestId,
  this.fee,
  this.discountedFee,
  this.bookingFee});

User.fromJson(Map<String, dynamic> json) {
  encPartnerId = json['EncPartnerId'];
  encTestId = json['EncTestId'];
  fee = json['Fee'];
  discountedFee = json['DiscountedFee'];
  bookingFee = json['BookingFee'];
}

Map<String, dynamic> toJson() {
  final Map<String, dynamic> data = new Map<String, dynamic>();
  data['EncPartnerId'] = this.encPartnerId;
  data['EncTestId'] = this.encTestId;
  data['Fee'] = this.fee;
  data['DiscountedFee'] = this.discountedFee;
  data['BookingFee'] = this.bookingFee;
  return data;
 }
}

class DataTableEg extends StatefulWidget {
const DataTableEg({Key? key}) : super(key: key);

@override
_DataTableEgState createState() => _DataTableEgState();
}

class _DataTableEgState extends State<DataTableEg> {
 User? user;

@override
void initState() {
  super.initState();
  getData();
 }

 void getData() {
 // final response=         //your api call
 // user=User.fromJson(response.body);
 }
 @override
 Widget build(BuildContext context) {
   return DataTable(columns: <DataColumn>[
    DataColumn(label: Text("EncPartnerId")),
    DataColumn(label: Text("EncTestId")),
    ], rows: <DataRow>[
    DataRow(cells: <DataCell>[
    DataCell(Text(user?.encPartnerId ?? '')),
    DataCell(Text(user?.encTestId ?? '')),
    ])
  ]);
}
}

【讨论】:

  • 我相信我的 json 响应不是数组列表对象。
  • 根据你的json创建模型,然后你可以在数据表中使用它
  • 再次正如我在问题中提到的那样,他们能够使用这个包 [pub.dev/packages/json_table] 因为他们的 Json 响应类似于 [ {"equipe1":"PSG","equipe2":"DIJON.......} ],但我的 Json 响应只是在 {"DiscountedFee": "80","BookingFee": "50"}
  • 我已经编辑了我的 ans 有帮助吗?还是你还卡住了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
相关资源
最近更新 更多