android - 未处理的异常:“int”类型不是“String”类型的子类型
问题描述
干杯,我正在尝试从 UI 中获取 TextField 值,将它们放入地图中,然后在每次单击考试创建屏幕中的“创建”按钮时将该地图放入“考试”集合中,我收到错误 [错误:颤振/lib/ui/ui_dart_state.cc(186)] 未处理的异常:“int”类型不是“String”类型的子类型,该文档未添加到 Firebase 集合中,并且未显示下一个屏幕,以下是有问题的文件:
CreateExam 文件
class CreatExam extends StatefulWidget {
@override
_CreatExamState createState() => _CreatExamState();
}
class _CreatExamState extends State<CreatExam> {
final _formKey = GlobalKey<FormState>();
DateTime pickedDate;
TimeOfDay pickedTime;
DatabaseService db= DatabaseService();
String title;
String examID;
Timestamp deadline;
double globalScore;
double duration;
bool _isLoading=false;
void initState(){
super.initState();
pickedDate=DateTime.now();
pickedTime=TimeOfDay.now();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: Colors.blue[100],
appBar: AppBar(
backgroundColor: Colors.blue[100],
elevation: 0,
title: Text("Create Exam",style: TextStyle(color: Colors.black),),
centerTitle: true,
leading: IconButton(
onPressed: (){
Navigator.pushNamed(context, '/ProfessorHomePage');
},
icon: Icon(Icons.arrow_back_ios),
color: Colors.black,
),
),
body: _isLoading ? Container (
child:Center(child: CircularProgressIndicator(),) ,
) :Form(
key: _formKey,
child: Container(
padding: EdgeInsets.all(20),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
TextFormField(
validator: (val) => val.isEmpty ? "Enter Exam title please" : null,
decoration: InputDecoration(
hintText: "Exam Title",
fillColor: Colors.white,
filled: true,
),
onChanged: (val){title=val;},
),
SizedBox(height:20),
TextFormField(
validator: (val) => val.isEmpty ? "Enter Exam duration please" : 60,
decoration: InputDecoration(
hintText: "Exam Duration (minutes)",
fillColor: Colors.white,
filled: true,
),
onChanged: (val){duration=double.parse(val);},
),
SizedBox(height:20),
TextFormField(
validator: (val) => val.isEmpty ? "Enter Exam Global Score please" : null,
decoration: InputDecoration(
hintText: "Global Score",
fillColor: Colors.white,
filled: true,
),
onChanged: (val){globalScore=double.parse(val);},
),
SizedBox(height:20),
Container(
color: Colors.white,
child: ListTile(
title:Text("Date limite : ${pickedDate.year} / ${pickedDate.month} / ${pickedDate.day}"),
trailing: Icon(Icons.calendar_today),
onTap: _pickDate,
),
),
SizedBox(height:20),
Container(
color: Colors.white,
child: ListTile(
title:Text("Heure limite : ${pickedTime.hour} : ${pickedTime.minute} "),
trailing: Icon(Icons.lock_clock),
onTap: _pickTime,
),
),
SizedBox(height:20),
Container(
width: MediaQuery.of(context).size.width/2,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.white,
),
child: TextButton(
onPressed: (){createExam();},
child: Text("Create",style:TextStyle(fontSize: 18,color: Colors.black) ,)
)
)
],
),
),
),
),
),
);
}
_pickDate() async {
DateTime date = await showDatePicker(context: context, initialDate: DateTime.now(), firstDate: DateTime.now(), lastDate: DateTime(2025));
if(date!=null){
setState(() {
pickedDate=date;
});
}
}
_pickTime() async {
TimeOfDay time = await showTimePicker(context: context, initialTime: TimeOfDay.now());
if(time!=null){
setState(() {
pickedTime=time;
});
}
}
createExam() async {
if(_formKey.currentState.validate()){
setState(() {
_isLoading=true;
});
var uuid=Uuid();
examID=uuid.v1().toString();
DateTime time = DateTime(pickedDate.year,pickedDate.month,pickedDate.day,pickedTime.hour,pickedTime.minute);
Timestamp date = Timestamp.fromDate(time);
Map<String,dynamic> examMap ={
"examID":examID,
"duration":duration,
"title":title,
"deadline":date,
"globalscore":globalScore,
};
await db.addExamToFirebase(examMap, examID).then((value){
setState(() {
_isLoading=false;
Navigator.of(context).pushReplacement(MaterialPageRoute(builder:(context)=> CreateQuestions(examID)));
});
});
}
}
}
数据库文件
class DatabaseService {
final String uid;
DatabaseService({this.uid});
//collection reference.
final CollectionReference usersCollection =
FirebaseFirestore.instance.collection('users');
final CollectionReference classroomsCollection =
FirebaseFirestore.instance.collection('classrooms');
final CollectionReference examsCOllection =
FirebaseFirestore.instance.collection("exams");
//The functions that will update the infos of the user.
Future updateUserData(
{String name,
String lastName,
String email,
String password,
String pfpPath,
String userName,
String role,
List classesList}) async {
return await usersCollection.doc(uid).set({
'name': name,
'lastname': lastName,
'email': email,
'password': password,
'pfpPath': pfpPath,
'username': userName,
'role': role,
'classeslist': classesList
});
}
// add to classes List in firebase
Future updateUserClasses(List classesList) async{
return await usersCollection.doc(uid).set({'classeslist':classesList});
}
Future addToUserClassesList(CustomClass classroom) async {
var userData = await usersCollection.doc(uid).get();
var list = userData.get('classeslist');
list.add(classroom);
await updateUserClasses(list);
}
//user data from snapshot.
CustomUser userDataFromSnapshot(DocumentSnapshot snapshot) {
CustomUser customUser = CustomUser();
customUser.setName = snapshot.get('name');
customUser.setLastName = snapshot.get('lastname');
customUser.setEmail = snapshot.get('email');
customUser.setPassword = snapshot.get('password');
customUser.setPfpPath = snapshot.get('pfpPath');
customUser.setUserName = snapshot.get('username');
customUser.setRole = snapshot.get('role');
customUser.setClassesList = snapshot.get('classeslist');
return customUser;
}
//user doc stream.
Stream<CustomUser> get customUserData {
return usersCollection.doc(uid).snapshots().map(userDataFromSnapshot);
}
// function for class creation in firebase
Future addClassroomToFirebase(String name,String subject,String password,String classID) async {
Map<String,dynamic> classroomInfo = {
"name":name,
"subject":subject,
"classID" : classID,
"classPassword":password,
"studentIDList":[]
};
return await classroomsCollection.add(classroomInfo);
}
//methode to fetch the class id
Future fetchID(String id) async {
classroomsCollection.snapshots().listen((snapshot) {
snapshot.docs.forEach((doc) {
if(id==doc.get("classID")){
return doc.id;
}
});
});
}
//get classrooms stream
Stream<QuerySnapshot> get classrooms {
return classroomsCollection.snapshots();
}
//upload Exam to firebase
Future addExamToFirebase(Map examData, String examID) async {
examsCOllection.doc(examID).set(examData)
.catchError((e){
print(e.toString());
});
}
//upload Questions to firebase
Future addQuestionstoFirebase(Map questionData, String examID) async {
await examsCOllection.doc(examID).collection("question").add(questionData);
}
}
这是调试控制台内容:
I/ViewRootImpl@99756eb[MainActivity]( 3207): ViewPostIme pointer 0
I/ViewRootImpl@99756eb[MainActivity]( 3207): ViewPostIme pointer 1
E/flutter ( 3207): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'int' is not a subtype of type 'String'
E/flutter ( 3207): #0 _CreatExamState.build.<anonymous closure>
E/flutter ( 3207): #1 FormFieldState._validate
E/flutter ( 3207): #2 FormFieldState.validate.<anonymous closure>
E/flutter ( 3207): #3 State.setState
E/flutter ( 3207): #4 FormFieldState.validate
E/flutter ( 3207): #5 FormState._validate
E/flutter ( 3207): #6 FormState.validate
E/flutter ( 3207): #7 _CreatExamState.createExam
E/flutter ( 3207): #8 _CreatExamState.build.<anonymous closure>
E/flutter ( 3207): #9 _InkResponseState._handleTap
E/flutter ( 3207): #10 GestureRecognizer.invokeCallback
E/flutter ( 3207): #11 TapGestureRecognizer.handleTapUp
E/flutter ( 3207): #12 BaseTapGestureRecognizer._checkUp
E/flutter ( 3207): #13 BaseTapGestureRecognizer.acceptGesture
E/flutter ( 3207): #14 GestureArenaManager.sweep
E/flutter ( 3207): #15 GestureBinding.handleEvent
E/flutter ( 3207): #16 GestureBinding.dispatchEvent
E/flutter ( 3207): #17 RendererBinding.dispatchEvent
E/flutter ( 3207): #18 GestureBinding._handlePointerEventImmediately
E/flutter ( 3207): #19 GestureBinding.handlePointerEvent
E/flutter ( 3207): #20 GestureBinding._flushPointerEventQueue
E/flutter ( 3207): #21 GestureBinding._handlePointerDataPacket
E/flutter ( 3207): #22 _rootRunUnary (dart:async/zone.dart:1370:13)
E/flutter ( 3207): #23 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter ( 3207): #24 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter ( 3207): #25 _invoke1 (dart:ui/hooks.dart:180:10)
E/flutter ( 3207): #26 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:276:7)
E/flutter ( 3207): #27 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
E/flutter ( 3207):
解决方案
当出现类型异常时,只需检查代码中是否存在错误的类型断言。检查这是否导致错误,验证器必须返回 a String
ornull
而您返回60
的是 a int
。尝试更改60
to"60"
并判断它是否有效:
TextFormField(
validator: (val) => val.isEmpty ? "Enter Exam duration please" : "60",
decoration: InputDecoration(
hintText: "Exam Duration (minutes)",
fillColor: Colors.white,
filled: true,
),
onChanged: (val){duration=double.parse(val);},
),
推荐阅读
- sql - 结合这4个SQL函数
- matlab - 使用新变量(不同维度)创建 NetCDF 文件
- google-chrome-extension - 在 Edge 上启用 page_action 图标但没有弹出窗口
- openapi-generator - 如何阻止某些类在 OpenApi 生成器中生成?
- sql - 获取周开始和结束日期
- r - 如何将功能从 Shiny App 移动到 Global.R 文件?
- java - 将两种不同的 xml 模式映射到一种通用 Java 对象模型的选项
- mongodb - 在一个聚合中从 mongodb 中的两个集合中查询数据
- python - Django / Python检查键是否存在,然后以高效和pythonic的方式有条件地更改键的值
- rest - HTTP 状态代码是 REST 的示例吗?