我正在使用两个 api,但我在 set State 和 jsonDecode 上出错,我在这里提到过
错误是 getter 'length' 被调用为 null。接收者:空尝试调用:长度
我正在使用两个 api,但我在 set State 和 jsonDecode 上出错,我在这里提到过
错误是 getter 'length' 被调用为 null。接收者:空尝试调用:长度
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:hospital/CartPage/pages/cartPage.dart';
import 'package:hospital/Drawer/dropdown_menu.dart';
import 'package:hospital/SecondSection/Appointment/AppointmentStatus/appointment_status.dart';
import 'package:hospital/constant.dart';
import 'package:hospital/customApiVariable.dart';
import 'package:http/http.dart' as http;
class Appointment extends StatefulWidget {
@override
_AppointmentState createState() => _AppointmentState();
}
class _AppointmentState extends State<Appointment> {
bool _isLoading = true;
List<dynamic> diseaseListJson;
List<dynamic> doctorListJson;
@override
void initState() {
super.initState();
fetchData();
}
Future fetchData() async {
setState(() {
_isLoading = true;
});
final results = await Future.wait([
http.get(Uri.parse(
'$hospitalBaseUrl/doctorListApi.php?a2rTokenKey=$a2rTokenKey')),
http.get(Uri.parse(
'$hospitalBaseUrl/diseaseCatApi.php.php?a2rTokenKey=$a2rTokenKey')),
]);
print(results[0]);
print(results[1]);
setState(() { //Here
diseaseListJson = jsonDecode(results[0].body); //Here
doctorListJson = jsonDecode(results[1].body);
//Here
});
setState(() {
_isLoading = false;
});
}
//select date part
DateTime selectedDate = DateTime.now();
Future<void> _selectDate(BuildContext context) async {
final DateTime picked = (await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101)));
if (picked != null && picked != selectedDate)
setState(() {
selectedDate = picked;
});
}
String diseaseChoose;
String doctorChoose;
//Select online/htc
List listOnline = [
// "Select Disease",
"Online",
"HTC",
];
String onlineChoose;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: kGreen,
title: Text(
"Book Appointment",
style: TextStyle(fontStyle: FontStyle.italic),
),
actions: [
IconButton(
icon: Icon(Icons.shopping_cart),
// onPressed: () => print("open cart"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Cartpage()),
);
},
),
DropDownMenu(),
],
),
body: SingleChildScrollView(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(15),
bottomLeft: Radius.circular(15),
),
),
elevation: 5,
child: Column(
children: [
//This is Select Disease part
Padding(
padding: const EdgeInsets.only(left: 26, right: 26, top: 20),
child: Card(
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(6.9),
),
elevation: 4,
shadowColor: kGreen,
child: Container(
padding: EdgeInsets.only(left: 10),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1),
borderRadius: BorderRadius.circular(10),
),
child: DropdownButton(
hint: Text("Select Disease"),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 36,
isExpanded: true,
underline: SizedBox(),
style: TextStyle(color: Colors.black, fontSize: 22),
value: diseaseChoose,
onChanged: (newValue) {
setState(() {
diseaseChoose = newValue;
});
},
items: List.generate(
diseaseListJson.length,
(index) => DropdownMenuItem(
value: diseaseListJson[index]['name'],
child: Text(diseaseListJson[index]['name']))
)),
),
),
),
Padding(padding: EdgeInsets.only(top: 20.0)),
//This is a select Date part
Padding(
padding: const EdgeInsets.only(left: 26, right: 26),
child: InkWell(
onTap: () {
setState(() {
_selectDate(context);
});
},
child: Card(
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(6.9),
),
elevation: 4,
shadowColor: kGreen,
child: Container(
height: 55,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1),
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 10),
child: Icon(Icons.calendar_today),
),
Padding(
padding: const EdgeInsets.only(left: 10),
child: Text(
// (paymentDate.toString()),
// ("${paymentDate.day}/${paymentDate.month}/${paymentDate.year}"),
"${selectedDate.toLocal()}".split(' ')[0],
style: TextStyle(color: Colors.black87),
),
),
],
),
),
),
),
),
Padding(padding: EdgeInsets.only(top: 20.0)),
//This is select top doctors part
Padding(
padding: const EdgeInsets.only(left: 26, right: 26),
child: Card(
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(6.9),
),
elevation: 4,
shadowColor: kGreen,
child: Container(
padding: EdgeInsets.only(left: 10),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1),
borderRadius: BorderRadius.circular(10),
),
child: DropdownButton(
hint: Text("Select Top Dr."),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 36,
isExpanded: true,
underline: SizedBox(),
style: TextStyle(color: Colors.black, fontSize: 22),
value: doctorChoose,
onChanged: (newValue) {
setState(() {
doctorChoose = newValue;
});
},
items: List.generate(
doctorListJson.length,
(index) => DropdownMenuItem(
value: doctorListJson[index]['doctor_name'],
child: Text(
doctorListJson[index]['doctor_name']))
))),
),
),
Padding(padding: EdgeInsets.only(top: 20.0)),
//This is select online/htc part
Padding(
padding: const EdgeInsets.only(left: 26, right: 26),
child: Card(
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(6.9),
),
elevation: 4,
shadowColor: kGreen,
child: Container(
padding: EdgeInsets.only(left: 10),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1),
borderRadius: BorderRadius.circular(10),
),
child: DropdownButton(
hint: Text("Online/HTC"),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 36,
isExpanded: true,
underline: SizedBox(),
style: TextStyle(color: Colors.black, fontSize: 22),
value: onlineChoose,
onChanged: (newValue) {
setState(() {
onlineChoose = newValue;
});
},
items: listOnline.map((valueOnline) {
return DropdownMenuItem(
value: valueOnline, child: Text(valueOnline));
}).toList(),
),
),
),
),
Padding(padding: EdgeInsets.only(top: 20.0)),
// Spacer(),
Padding(
padding: const EdgeInsets.only(left: 210.0, bottom: 20),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
),
width: 140,
height: 53,
child: RaisedButton(
elevation: 5,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AppointmentStatus()),
);
},
color: kGreen,
child: Text(
"Book & Pay",
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
),
),
),
],
),
),
));
}
}
答案 0 :(得分:0)
我认为您在初始化之前正在使用该列表。 您需要在声明列表时对其进行初始化。 否则会抛出空错误。
List<dynamic> diseaseListJson=[]; List<dynamic> doctorListJson=[];