[英]Flutter: value of type 'Future<List<UserVideo>>' can't be assigned to a variable of type 'List<UserVideo>'
I am trying to use one List (custom type) but getting error.我正在尝试使用一个列表(自定义类型)但出现错误。
When i try to use the getData() function.当我尝试使用 getData() function。 Like below.
如下所示。
List<UserVideo> videoDataList = [];
videoDataList = UserVideo.getData();
This is initState method.这是 initState 方法。
@override
void initState() {
videoDataList = await UserVideo.getData();
WidgetsBinding.instance.addObserver(this);
_videoListController.init(
_pageController,
videoDataList,
);
super.initState();
}
I am getting the error.我收到错误消息。
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>'.
Here is the code for function.这是 function 的代码。
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;
}
Edit:编辑:
Just showing the hardcoded value which is working fine.只是显示工作正常的硬编码值。
static List<UserVideo> fetchVideo() {
List<UserVideo> list = [];
list.add(UserVideo(image: '', url: mockVideo, desc: 'Test1'));
list.add(UserVideo(image: '', url: mV2, desc: 'MV_TEST_2'));
list.add(UserVideo(image: '', url: mV3, desc: 'MV_TEST_3'));
list.add(UserVideo(image: '', url: mV4, desc: 'MV_TEST_4'));
return list;
}
I can use it like this and no error.我可以像这样使用它并且没有错误。
videoDataList = UserVideo.fetchVideo();
Your method getData()
returns a Future:您的方法
getData()
返回一个 Future:
Future<List<UserVideo>> getData() async {
List<UserVideo> list = [];
try {
var deviceid = '123';
var dtgUid = '100';
var nodata;
var bodyss = {
"uid": dtgUid,
"deviceid": deviceid,
};
You have to use async/await
to call the method getData()
:您必须使用
async/await
来调用方法getData()
:
List<UserVideo> videoDataList = [];
videoDataList = await UserVideo.getData();
or use then()
:或使用
then()
:
List<UserVideo> videoDataList = [];
UserVideo.getData().then((list){
videoDataList = list;
});
Note: To use await
you need to declare a method async
注意:要使用
await
你需要声明一个方法async
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.