刷新页面时,此错误首先开始,然后开始起作用。
错误:在null上调用了getter'isNotEmpty'。 接收者:null 尝试调用:isNotEmpty
class TreePage extends StatefulWidget {
String vp;
TreePage({this.vp});
@override
_TreePageState createState() => _TreePageState();
}
class _TreePageState extends State<TreePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('Проверка штрафов',
style: TextStyle(color: Colors.white,),
),
centerTitle: true,
),
//new return Container(),
body: new FutureBuilder(
future: getPhoneFinal(widget.vp),
//future: codePhone(widget.code),
builder: (context, snapshot){
List data = snapshot.data;
Map shtr = getByKeySh();
Map shtrIm = getByKeyIm();
Map violQnt;
if(data.isNotEmpty){
violQnt = getViolationQnt(data);
}else{
violQnt = null;
}
if(snapshot.hasError){
print (snapshot.error);
return Text('Не удалось получить ответ от сервера',
style: TextStyle(color: Colors.red,
fontSize: 22.0)
);
}
else if(snapshot.hasData ){
return ListView.builder (
itemCount: snapshot.data.length, //length of snapshot data
itemBuilder: (BuildContext ctxt, int index) {
return Container(
child:Column(
children: <Widget>[
(index == 0)?
new Container(
child:Column(
children: <Widget>[
new Text('Подтвержденные штрафы: ${violQnt['approved_qnt']}'),
]
),
)
: new Container(),
new Container(
child: Image.asset(
'images/${shtrIm[data[index] ['VId']]}',
width: 50,
height: 50,
),
),
new Container(
child: Image.asset(
'images/${(data[index] ['ProcessStatus']==1)? 'sts-ok.png': 'sts-no.png'}',
width: 50,
height: 50,
),
),
new Container(
child: new Text(
'${(data[index] ['IsPaid']==1)? 'Да': 'нет'}',
),
),
new Container(
child: new Text(
//'Тип штрафа: ${data[index]['VDescription']}'
'Тип штрафа: ${shtr[data[index] ['VId']]}'
),
),
new Container(
child: new Text(
'Адрес: ${data[index]['VLocation']}'
),
),
new Container(
child: new Text(
'Дата: ${data[index]['VTime']}'
),
),
],
),
);
}
);
}else if(!snapshot.hasData){
return new Center(child: CircularProgressIndicator(),);
}
}),
);
}
// getFine(String vp) {}
}
Future<Map> getPhone(String phone,String code) async{
String url = 'xxx/xxx/$phone/$code';
http.Response response = await http.get(url);
//var sts = response.statusCode;
//log( response.statusCode);
// var ar = {
// 'sts': response.statusCode,
//'data': json.decode(response.body)
//};
return json.decode(response.body);
}
Future<List> getPhoneFinal(String numbCar) async{
String url = 'xxx/xxx/$numbCar';
http.Response response = await http.get(url);
return json.decode(utf8.decode(response.bodyBytes));
}
Map<String, String> getByKeySh() {
return {
'1625': 'Проезд на красный сигнал светофора',
'1302': 'Проезд на красный сигнал светофора',
'1230': 'Пересечение сплошной линии',
'1301': 'Движение по встречной полосе',
//1301 => 'Выезд на полосу встречного движения',
'1345': 'Выезд за стоп-линию'
};
}
Map<String, String> getByKeyIm() {
return {
'1625': '1625.png',
'1302': '1302.png',
'1230': '1230.png',
'1301': '1301.png',
//1301 => '',
'1345': '1345.png'
};
}
Map<String, int> getViolationQnt(List data ) {
var total_qnt = 0;
var approved_qnt = 0;
var paid_qnt = 0;
var process_qnt = 0;
// if(data.isNotEmpty){
for(var i=0; i< data.length; i++){
total_qnt++;
if(data[i]['IsPaid'] == 1){
paid_qnt++;
}
if(data[i]['IsPaid'] == 0 && data[i]['ProcessStatus'] == 1){
approved_qnt++;
}
if(data[i]['IsPaid'] == 0 && data[i]['ProcessStatus'] != 1){
process_qnt++;
}
}
// }
return {
'total_qnt': total_qnt,
'approved_qnt': approved_qnt,
'paid_qnt': paid_qnt,
'process_qnt': process_qnt,
};
}
答案 0 :(得分:1)
请使用data.length > 0
代替data.isNotEmpty()也可以使用snapshot.hasData
进行检查
答案 1 :(得分:0)
当您FutureBuilder
首次没有数据时调用它的构建器方法。所以你应该先检查一下。
FutureBuilder(
future: getPhoneFinal(widget.vp),
builder: (context, snapshot){
if(snapshot.hasData){
List data = snapshot.data;
...
}else{
return Container(); //or show a loading spinner
}
答案 2 :(得分:0)
使用FutureBuilder时遇到了相同的问题。在检索结果之前,我有些延迟。在您的情况下,List数据为空,因为尚未收到响应,这可以证明您的错误是正确的,并且在接收到数据后,它就可以工作。您可能想要添加一个加载器,等待进一步的响应