【问题标题】:Flutter: value of type 'Future<List<UserVideo>>' can't be assigned to a variable of type 'List<UserVideo>'Flutter:“Future<List<UserVideo>>”类型的值不能分配给“List<UserVideo>”类型的变量
【发布时间】:2020-10-24 21:37:39
【问题描述】:

我正在尝试使用一个列表(自定义类型)但出现错误。

当我尝试使用 getData() 函数时。如下所示。

List<UserVideo> videoDataList = [];

videoDataList = UserVideo.getData(); 

这是 initState 方法。

@override
  void initState() {
    videoDataList = await UserVideo.getData();
    WidgetsBinding.instance.addObserver(this);
    _videoListController.init(
      _pageController,
      videoDataList,
   
    );

    super.initState();
  }

我收到了错误。

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

这是函数的代码。

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;
  }

编辑:

只显示正常工作的硬编码值。

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;
  } 

我可以这样使用它并且没有错误。

videoDataList = UserVideo.fetchVideo();

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您的方法 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,
    
          };
    

    你必须使用async/await来调用方法getData()

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

    或使用then():

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

    注意:要使用await,您需要声明一个方法async

    【讨论】:

    • 感谢彼得的回答。让我试试。我试图在 Init 方法中使用它。用更多代码更新问题。等待给出错误。 await 表达式只能在异步函数中使用。尝试用“async”或“async*”标记函数体。dart(await_in_wrong_context)
    • 我检查了编辑并尝试了 then 方法。现在它给出错误实例成员'getData'不能使用静态访问访问。
    • 你需要创建一个UserVideo类的实例,然后调用getData
    • 我认为 Class 已经创建好了。检查我的问题。如果我对值进行硬编码,则不会出错。添加硬编码值的代码。它在问题结束时更新。
    • 你需要创建一个类(对象)的实例,你不能这样调用getData()。你必须先做var userVideo = UserVideo()然后再做userVideo.getData()
    猜你喜欢
    • 2021-08-29
    • 2021-10-08
    • 2020-06-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多