从迁移到 null-safety 我有 3 个问题
I have 3 issues from migrating to null-safety
我今天迁移到了 null-safety,但我有 3 个问题不知道如何解决。下面,我提供了错误以及每个错误所指的代码。我不知道如何解决这些问题,它们是唯一剩下的错误。
error • The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type at lib\Services\firestore_service.dart:29:16 • (body_might_complete_normally)
Future<Void> saveNewAgency(Agency agency) async {
await _db.collection('agency').doc(globals.agencyId).set(agency.toMap());
}
error • The property 'docs' can't be unconditionally accessed because the receiver can be 'null' at lib\screens\agent_dash_board.dart:57:43 •(unchecked_use_of_nullable_value)
I fixed similar errors to this one with the code at itemCount: (snapshot.data! as QuerySnapshot).docs.length, in the code snippet.
return new ListView.builder(
itemCount: (snapshot.data! as QuerySnapshot).docs.length,
itemBuilder: (BuildContext context, int index) {
Trxns trxns = Trxns.fromFirestore(
snapshot.data.docs[index].data());
error • The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type at lib\screens\appointment_calendar.dart:434:31 • (body_might_complete_normally)
Map<DateTime, List<Event?>> convertToMap(List<Event> item) {
Map<DateTime, List>? result;
for (int i = 0; i < item.length; i++) {
Event data = item[i];
//get the date and convert it to a DateTime variable
//DateTime currentDate = DateFormat('MM-dd-yyyy - kk:mm').format(data.eventDate);
DateTime currentDate = DateTime(data.eventDate!.year, data.eventDate!.month, data.eventDate!.day, data.eventDate!.hour, data.eventDate!.minute);
List eventNames = [];
//add the event name to the the eventNames list for the current date.
//search for another event with the same date and populate the eventNames List.
for (int j = 0; j < item.length; j++) {
//create temp calendarItemData object.
Event temp = item[j];
//establish that the temp date is equal to the current date
if (data.eventDate == temp.eventDate) {
//add the event name to the event List.
eventNames.add(temp.eventName);
} //else continue
}
//add the date and the event to the map if the date is not contained in the map
if (result == null) {
result = {
currentDate: eventNames
};
} else {
result[currentDate] = eventNames;
}
return result as Map<DateTime, List<Event?>>;
}
}
Future<Void> saveNewAgency(Agency agency) async {
await _db.collection('agency').doc(globals.agencyId).set(agency.toMap());
}
试试 returning Future<void>
我不确定你从哪里得到 Void
大写字母 V 来自。
return new ListView.builder(
itemCount: (snapshot.data! as QuerySnapshot).docs.length,
itemBuilder: (BuildContext context, int index) {
Trxns trxns = Trxns.fromFirestore(
snapshot.data.docs[index].data());
下面讨论的解决方案是这样做的:
Trxns trxns = Trxns.fromFirestore((snapshot.data! as QuerySnapshot).docs[index]!.data() as Map<String, dynamic>);
Map<DateTime, List<Event?>> convertToMap(List<Event> item) {
Map<DateTime, List>? result;
for (int i = 0; i < item.length; i++) {
Event data = item[i];
//get the date and convert it to a DateTime variable
//DateTime currentDate = DateFormat('MM-dd-yyyy - kk:mm').format(data.eventDate);
DateTime currentDate = DateTime(data.eventDate!.year, data.eventDate!.month, data.eventDate!.day, data.eventDate!.hour, data.eventDate!.minute);
List eventNames = [];
//add the event name to the the eventNames list for the current date.
//search for another event with the same date and populate the eventNames List.
for (int j = 0; j < item.length; j++) {
//create temp calendarItemData object.
Event temp = item[j];
//establish that the temp date is equal to the current date
if (data.eventDate == temp.eventDate) {
//add the event name to the event List.
eventNames.add(temp.eventName);
} //else continue
}
//add the date and the event to the map if the date is not contained in the map
if (result == null) {
result = {
currentDate: eventNames
};
} else {
result[currentDate] = eventNames;
}
return result as Map<DateTime, List<Event?>>;
}
}
我想你是想把 return 语句放在 for 循环之后,而不是在它里面。
我今天迁移到了 null-safety,但我有 3 个问题不知道如何解决。下面,我提供了错误以及每个错误所指的代码。我不知道如何解决这些问题,它们是唯一剩下的错误。
error • The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type at lib\Services\firestore_service.dart:29:16 • (body_might_complete_normally)
Future<Void> saveNewAgency(Agency agency) async {
await _db.collection('agency').doc(globals.agencyId).set(agency.toMap());
}
error • The property 'docs' can't be unconditionally accessed because the receiver can be 'null' at lib\screens\agent_dash_board.dart:57:43 •(unchecked_use_of_nullable_value)
I fixed similar errors to this one with the code at itemCount: (snapshot.data! as QuerySnapshot).docs.length, in the code snippet.
return new ListView.builder(
itemCount: (snapshot.data! as QuerySnapshot).docs.length,
itemBuilder: (BuildContext context, int index) {
Trxns trxns = Trxns.fromFirestore(
snapshot.data.docs[index].data());
error • The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type at lib\screens\appointment_calendar.dart:434:31 • (body_might_complete_normally)
Map<DateTime, List<Event?>> convertToMap(List<Event> item) {
Map<DateTime, List>? result;
for (int i = 0; i < item.length; i++) {
Event data = item[i];
//get the date and convert it to a DateTime variable
//DateTime currentDate = DateFormat('MM-dd-yyyy - kk:mm').format(data.eventDate);
DateTime currentDate = DateTime(data.eventDate!.year, data.eventDate!.month, data.eventDate!.day, data.eventDate!.hour, data.eventDate!.minute);
List eventNames = [];
//add the event name to the the eventNames list for the current date.
//search for another event with the same date and populate the eventNames List.
for (int j = 0; j < item.length; j++) {
//create temp calendarItemData object.
Event temp = item[j];
//establish that the temp date is equal to the current date
if (data.eventDate == temp.eventDate) {
//add the event name to the event List.
eventNames.add(temp.eventName);
} //else continue
}
//add the date and the event to the map if the date is not contained in the map
if (result == null) {
result = {
currentDate: eventNames
};
} else {
result[currentDate] = eventNames;
}
return result as Map<DateTime, List<Event?>>;
}
}
Future<Void> saveNewAgency(Agency agency) async {
await _db.collection('agency').doc(globals.agencyId).set(agency.toMap());
}
试试 returning Future<void>
我不确定你从哪里得到 Void
大写字母 V 来自。
return new ListView.builder(
itemCount: (snapshot.data! as QuerySnapshot).docs.length,
itemBuilder: (BuildContext context, int index) {
Trxns trxns = Trxns.fromFirestore(
snapshot.data.docs[index].data());
下面讨论的解决方案是这样做的:
Trxns trxns = Trxns.fromFirestore((snapshot.data! as QuerySnapshot).docs[index]!.data() as Map<String, dynamic>);
Map<DateTime, List<Event?>> convertToMap(List<Event> item) {
Map<DateTime, List>? result;
for (int i = 0; i < item.length; i++) {
Event data = item[i];
//get the date and convert it to a DateTime variable
//DateTime currentDate = DateFormat('MM-dd-yyyy - kk:mm').format(data.eventDate);
DateTime currentDate = DateTime(data.eventDate!.year, data.eventDate!.month, data.eventDate!.day, data.eventDate!.hour, data.eventDate!.minute);
List eventNames = [];
//add the event name to the the eventNames list for the current date.
//search for another event with the same date and populate the eventNames List.
for (int j = 0; j < item.length; j++) {
//create temp calendarItemData object.
Event temp = item[j];
//establish that the temp date is equal to the current date
if (data.eventDate == temp.eventDate) {
//add the event name to the event List.
eventNames.add(temp.eventName);
} //else continue
}
//add the date and the event to the map if the date is not contained in the map
if (result == null) {
result = {
currentDate: eventNames
};
} else {
result[currentDate] = eventNames;
}
return result as Map<DateTime, List<Event?>>;
}
}
我想你是想把 return 语句放在 for 循环之后,而不是在它里面。