简体   繁体   中英

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. Like below.

List<UserVideo> videoDataList = [];

videoDataList = UserVideo.getData(); 

This is initState method.

@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.

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:

 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() :

List<UserVideo> videoDataList = [];
videoDataList = await UserVideo.getData(); 

or use then() :

List<UserVideo> videoDataList = [];
UserVideo.getData().then((list){
   videoDataList = list;
 });

Note: To use await you need to declare a method async

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM