类型 'Future<List<Appointment>>' 不是类型转换中类型 'List<Appointment>' 的子类型
type 'Future<List<Appointment>>' is not a subtype of type 'List<Appointment>' in type cast
错误应该很清楚,但我不确定如何解决。
基本上我有一个 Stream 构建器,我每秒通过 getData() 方法调用以用新数据更新我的 SfCalendar。
Stream<DataSource> getData() async* {
await Future.delayed(const Duration(seconds: 1)); //Mock delay
List<Appointment> appointments = foo() as List<Appointment>;
List<CalendarResource> resources = bar() as List<CalendarResource>;
DataSource data = DataSource(appointments, resources);
print("Fetched Data");
yield data;
}
但是我的约会方法 foo() 是 Future 类型而不是 List。
Future<List<Appointment>> foo() async {
var url0 = Uri.https(
"uri",
"/profiles.json");
List<Appointment> appointments = [];
try {
final response = await dio.get(url0.toString());
//final Random random = Random();
//_colorCollection[random.nextInt(9)];
response.data.forEach((key, value) {
appointments.add(
Appointment(
id: int.parse(
value["id"],
),
startTime: DateTime.parse(value["startTime"]),
endTime: DateTime.parse(value["endTime"]),
),
);
});
} catch (error) {
print(error);
}
return appointments;
}
这就是错误应该告诉的内容,是吗?
我尝试从 foo() 约会中删除 Future 演员表,但我无法使用异步。
我也尝试返回 Future.value(appointments) 但同样的错误。
这是我在 initState() 中调用流的地方:
@override
void initState() {
super.initState();
print("Creating a sample stream...");
Stream<DataSource> stream = getData();
print("Created the stream");
stream.listen((data) {
print("DataReceived");
}, onDone: () {
print("Task Done");
}, onError: (error) {
print(error);
});
print("code controller is here");
}
谢谢,可能的话请帮忙
就像 JavaScript 一样,异步功能总是 return Future。这就是为什么从 return 类型中删除 Future 时不能使用异步的原因。
由于您不是在等待 Future 解析,您实际上是在尝试将 Future 转换为 List,这不是有效的转换。您需要做的就是等待函数完成,以便它解析为一个列表:
List<Appointment> appointments = await foo() as List<Appointment>;
并且,由于您的 return 类型是 Future>,您实际上不需要转换结果。
List<Appointment> appointments = await foo();
错误应该很清楚,但我不确定如何解决。
基本上我有一个 Stream 构建器,我每秒通过 getData() 方法调用以用新数据更新我的 SfCalendar。
Stream<DataSource> getData() async* {
await Future.delayed(const Duration(seconds: 1)); //Mock delay
List<Appointment> appointments = foo() as List<Appointment>;
List<CalendarResource> resources = bar() as List<CalendarResource>;
DataSource data = DataSource(appointments, resources);
print("Fetched Data");
yield data;
}
但是我的约会方法 foo() 是 Future 类型而不是 List。
Future<List<Appointment>> foo() async {
var url0 = Uri.https(
"uri",
"/profiles.json");
List<Appointment> appointments = [];
try {
final response = await dio.get(url0.toString());
//final Random random = Random();
//_colorCollection[random.nextInt(9)];
response.data.forEach((key, value) {
appointments.add(
Appointment(
id: int.parse(
value["id"],
),
startTime: DateTime.parse(value["startTime"]),
endTime: DateTime.parse(value["endTime"]),
),
);
});
} catch (error) {
print(error);
}
return appointments;
}
这就是错误应该告诉的内容,是吗? 我尝试从 foo() 约会中删除 Future 演员表,但我无法使用异步。 我也尝试返回 Future.value(appointments) 但同样的错误。
这是我在 initState() 中调用流的地方:
@override
void initState() {
super.initState();
print("Creating a sample stream...");
Stream<DataSource> stream = getData();
print("Created the stream");
stream.listen((data) {
print("DataReceived");
}, onDone: () {
print("Task Done");
}, onError: (error) {
print(error);
});
print("code controller is here");
}
谢谢,可能的话请帮忙
就像 JavaScript 一样,异步功能总是 return Future。这就是为什么从 return 类型中删除 Future 时不能使用异步的原因。
由于您不是在等待 Future 解析,您实际上是在尝试将 Future 转换为 List,这不是有效的转换。您需要做的就是等待函数完成,以便它解析为一个列表:
List<Appointment> appointments = await foo() as List<Appointment>;
并且,由于您的 return 类型是 Future>,您实际上不需要转换结果。
List<Appointment> appointments = await foo();