【发布时间】:2020-11-29 10:23:34
【问题描述】:
我有来自数据库的数据类:
class Months {
final int index;
final String activity_k;
final String activity_a;
final String udigree;
Months(this.index, this.activity_k, this.activity_a, this.udigree);
}
我有这个页面请求数据并在数据表上查看它们:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:jiyanUquraan/classes/monthly.dart';
class MonthlyReport extends StatefulWidget {
@override
_MonthlyReportState createState() => _MonthlyReportState();
}
class _MonthlyReportState extends State<MonthlyReport> {
var selectedCourse = '1';
@override
Widget build(BuildContext context) {
var widthView = MediaQuery.of(context).size.width;
List monthly = [];
Future<List> fetchReport() async {
Map rdata = ModalRoute.of(context).settings.arguments;
var u_id = rdata['u_id'];
var lang = rdata['lang'];
print('lang=$lang');
print('uid=$u_id');
print('course=$selectedCourse');
var url =
'http://10.0.2.2/jiyan/test/api/digrees/monthly_report.php?u_id=$u_id&m_id=$selectedCourse';
var response = await http.get(url);
var data = jsonDecode(response.body);
print(data);
for (var x in data) {
Months _monthlyreport =
Months(x['index'], x['activity_k'], x['activity_a'], x['udigree']);
monthly.add(_monthlyreport);
return monthly;
}
}
return FutureBuilder(
future: fetchReport(),
builder: (context, snapshot) {
List monthly = snapshot.data;
if (snapshot.data == null) {
return Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(15, 20, 15, 20),
child: Row(
children: <Widget>[
Text('ژمارا کورسی ژێ بگرە',
style: TextStyle(fontSize: 22, color: Colors.red)),
Container(
width: 30,
),
Container(
width: 60,
height: 40,
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
color: Color.fromRGBO(230, 200, 200, 0.2),
borderRadius: BorderRadius.circular(10)),
child: buildDropDownFieldList()),
],
),
),
Text(
"ڤی کورسی هێشتا راپورت نینە",
style: TextStyle(fontSize: 32, color: Colors.white),
),
],
);
} else {
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: ListView(
padding: EdgeInsets.fromLTRB(15, 20, 15, 20),
shrinkWrap: true,
children: <Widget>[
Row(
children: <Widget>[
Text('ژمارا کورسی ژێ بگرە',
style: TextStyle(fontSize: 22, color: Colors.red)),
Container(
width: 50,
),
Container(
width: 60,
height: 40,
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
color: Color.fromRGBO(230, 200, 200, 0.2),
borderRadius: BorderRadius.circular(10)),
child: buildDropDownFieldList()),
],
),
SizedBox(
height: 15,
),
Directionality(
textDirection: TextDirection.rtl,
child: DataTable(
columns: [
DataColumn(
label: Center(
child: Text('ئەرک',
style: TextStyle(
fontSize: 26,
color: Colors.white)))),
DataColumn(
label: Center(
child: Text('نمۆە',
style: TextStyle(
fontSize: 26,
color: Colors.white)))),
],
rows: monthly
.map((items) => DataRow(cells: [
DataCell(Text(items.activity_k)),
DataCell(Text(items.udigree)),
]))
.toList()
//End of List
)),
]),
);
}
});
}
DropdownButton buildDropDownFieldList() {
return DropdownButton<String>(
value: selectedCourse,
elevation: 16,
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.bold, fontSize: 22),
underline: SizedBox(),
onChanged: (String newValue) {
setState(() {
selectedCourse = newValue;
});
},
items: <String>['1','2','3','4','5','6','7','8','9','10','11','12','13'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Center(child: Text(value)),
);
}).toList(),
);
}
}
String selectedCourse = '1';
当我更改下拉列表时,数据更改正确,并且我尝试从 URL 打印数据响应,它已成功获取数据,但表数据仅查看一行,我知道它们不止一行。 当我用 snapshot.data.map((items)) 更改monthly.map((items) 时,出现此错误:
type 'List<dynamic>' is not a subtype of type 'List<DataRow>'
【问题讨论】:
-
可以分享
monthly的内容样本吗? -
我现在将在问题中添加更多细节
-
@Mobina 我用更多细节更改了问题
-
@Mobina 你有什么清楚的吗?
-
我刚刚发布了一个答案。如果有帮助,请告诉我。