我正在尝试使用一个列表(自定义类型),但出现错误。 在这两行上。 List和getData在VSCode中显示为红色,类似错误。
Future List<UserVideo> getData() async {
List<UserVideo> list = [];
这是我得到的错误。
This function has a return type of 'Future<dynamic>', but doesn't end with a return statement.
Try adding a return statement, or changing the return type to 'void'.dartmissing_re
这是代码。
class UserVideo {
final String url;
final String image;
final String desc;
UserVideo({
this.url: mockVideo,
this.image: mockImage,
this.desc,
});
Future List<UserVideo> getData() async {
List<UserVideo> list = [];
try {
var deviceid = '123';
var dtgUid = '100';
var nodata;
var bodyss = {
"uid": dtgUid,
"deviceid": deviceid,
};
var url = 'http://192.168.100.4:8080/videos/get-data.php';
// Starting Web API Call.
var response = await http
.post(url, body: json.encode(bodyss))
.timeout(Duration(seconds: 5), onTimeout: () {
return null;
});
if (response.statusCode == 200) {
final data = StreamingFromJson(response.body);
if (data.count == null) {
count = 0;
} else {
count = data.count;
}
if (data.content.length > 0 && data.content[0].name != 'Empty') {
for (var i in data.content) {
list.add(UserVideo(image: i.thumbnail, url: i.video, desc: i.title));
}
} else {
nodata = 'No Record Found';
}
print(list.length);
}
} catch (e) {
print("Exception Caught: $e");
}
return list;
}
还有另外几个错误,如下所示。
在列表中
Name non-constant identifiers using lowerCamelCase.dartnon_constant_identifier_names
This function has a return type of 'Future<dynamic>', but doesn't end with a return statement.
Try adding a return statement, or changing the return type to 'void'.dartmissing_return
Functions must have an explicit list of parameters.
Try adding a parameter list.dart(missing_function_parameters)
关于getData函数
A function body must be provided.
Try adding a function body.
修改
JayDev帮助我解决了该错误,但现在又给了我另一个错误。
当我尝试使用getData()函数时。如下所示。
List<UserVideo> videoDataList = [];
videoDataList = UserVideo.getData();
我收到错误消息。
A value of type 'Future<List<UserVideo>>' can't be assigned to a variable of type 'List<UserVideo>'.
Try changing the type of the variable, or casting the right-hand type to 'List<UserVideo>'.
答案 0 :(得分:2)
我相信您的方法签名是错误的。您应该将想要从Future返回的类型放在大括号后,即
Future<List<UserVideo>> getData() async {
...
}