【问题标题】:error: A value of type 'RxList<Proje>' can't be assigned to a variable of type 'List<Proje>'错误:“RxList<Proje>”类型的值不能分配给“List<Proje>”类型的变量
【发布时间】:2021-04-21 11:08:40
【问题描述】:

使用 Get 包作为状态管理,我编写了一个昨天工作的控制器。现在它在哭。 这是定义:

class ProjeController extends GetxController {
  RxList<Proje> projeList = <Proje>[].obs;

  @override
  void onInit() {
    List storedProjes = GetStorage().read<List>('projelist');
    if (storedProjes != null) {
      projeList = storedProjes.map((e) => Proje.fromJson(e)).toList().obs;
    }

当我尝试将列表分配给此处的变量时:

final ProjeController pc = Get.find();
List<Proje> pl = pc.projeList;

我收到此错误: 错误:“RxList”类型的值不能分配给“List”类型的变量。 ([esrefmasraf] lib\screens\projeeklesil.dart:16 处的无效分配)

我试过List&lt;Proje&gt;.empty(growable:true).obs,在破坏代码的其他部分的同时给出了同样的错误。

我很确定它昨天还在工作,但历史显示代码没有变化。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    刚刚更新:

    List<Proje> pl = pc.projeList.toList();
    
    

    【讨论】:

    • 以前也这样做过,返回错误:'无法分配'List类型的值(其中Proje在\esrefmasraf\lib\models\proje.dart中定义)'到 'List 类型的变量(其中 Proje 在 \esrefmasraf\lib\models\proje.dart 中定义)'。 ([esrefmasraf] lib\screens\projeeklesil.dart:16 处的无效分配)'
    • 您能否检查一下您是否使用了相对导入? (应该使用 'package:...' 进口)
    • 太棒了!我就是这么做的。谢谢。那些很棒的进口只花了几个小时,还不错:(
    • 是的,这是 Dart 中的一个偷偷摸摸的错误
    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多