简体   繁体   English

Flutter:“未来”类型的值<list<uservideo> &gt;' 不能分配给“列表”类型的变量<uservideo> ' </uservideo></list<uservideo>

[英]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.

相关问题 Future builder flutter firebase 错误:“Iterable”类型的值<BlogPost> &#39; 不能分配给类型为 &#39;List 的变量<BlogPost> &#39; - Future builder flutter firebase error: A value of type 'Iterable<BlogPost>' can't be assigned to a variable of type 'List<BlogPost>' Flutter:“列表”类型的值<type> ? 不能分配给“列表”类型的变量<type> '</type></type> - Flutter : A value of type 'List<Type>?' can't be assigned to a variable of type 'List<Type>' &#39;Future 类型的值<List<Question> &gt;&#39; 不能分配给类型为 &#39;List 的变量<Question> &#39; - A value of type 'Future<List<Question>>' can't be assigned to a variable of type 'List<Question>' “列表”类型的值<customer> ' 不能分配给 'Future' 类型的变量<list<customer> >' </list<customer></customer> - A value of type 'List<Customer>' can't be assigned to a variable of type 'Future<List<Customer>>' &#39;Future 类型的值<List<Questoes> &gt;&#39; 不能分配给“列表”类型的变量<Questoes> &#39; - A value of type 'Future<List<Questoes>>' can't be assigned to a variable of type 'List<Questoes>' 'Rx 类型的值<future<list<sectionsdbstat> &gt;*&gt;*' 不能分配给类型变量</future<list<sectionsdbstat> - A value of type 'Rx<Future<List<SectionsDBStat>>*>*' can't be assigned to a variable of type Flutter - 'List 类型的值<map<string, object> &gt;' 不能分配给“列表”类型的变量<classes> ' </classes></map<string,> - Flutter - A value of type 'List<Map<String, Object>>' can't be assigned to a variable of type 'List<Classes>' Flutter/Firestore - 错误:“列表”类型的值<string> Function(QuerySnapshot)' 不能分配给 'List' 类型的变量<dynamic> '</dynamic></string> - Flutter/Firestore - Error: A value of type 'List<String> Function(QuerySnapshot)' can't be assigned to a variable of type 'List<dynamic>' 'List?' 类型的值不能分配给“列表”类型的变量 Flutter Retrofit - A value of type 'List?' can't be assigned to a variable of type 'List' Flutter Retrofit 'List 类型的值<symptoms> ? 不能分配给“列表”类型的变量<symptoms> ' Flutter</symptoms></symptoms> - A value of type 'List<Symptoms>?' can't be assigned to a variable of type 'List<Symptoms>' Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM