如何解决这个错误? 'List<Word?>' 不是 'val'E/flutter (29035) 的 'List<Word>' 类型的子类型:#0 RxObjectMixin

Posted

技术标签:

【中文标题】如何解决这个错误? \'List<Word?>\' 不是 \'val\'E/flutter (29035) 的 \'List<Word>\' 类型的子类型:#0 RxObjectMixin.value=【英文标题】:How to solve this error? 'List<Word?>' is not a subtype of type 'List<Word>' of 'val'E/flutter (29035): #0 RxObjectMixin.value=如何解决这个错误? 'List<Word?>' 不是 'val'E/flutter (29035) 的 'List<Word>' 类型的子类型:#0 RxObjectMixin.value= 【发布时间】:2021-11-14 00:09:02 【问题描述】:

我在 Controller 类中定义了一个名为 data 的变量。我在 fillWordlist 方法中收到了上面提到的错误。我在视图类的 historyWordList 方法中创建了 listview.builder。但列表没有出现在屏幕上,我收到此错误。 'List' 不是 'val'E/flutter (29035) 的 'List' 类型的子类型:#0 RxObjectMixin.value= (package:get/get_rx/src/rx_types/rx_core/rx_impl .darts) E/flutter (29035): #1 WordController.fillWordList (package:your_research_translation/controller/word_controller.dart:56:10)'

控制器类

class WordController extends GetxController 
  TextEditingController controllerInput1 = TextEditingController();
  TextEditingController controllerInput2 = TextEditingController();
  RxBool active2 = false.obs;
  final translator = GoogleTranslator();
  RxList data = <Word>[].obs;

  @override
  void onInit() 
    fillWordList();
    super.onInit();
  

  onClickIconButtonFavori() 
    if (controllerInput1.text.isNotEmpty && controllerInput2.text.isNotEmpty) 
      addNote();
      clear();
    
  

  showText() 
    if (active2.value == true) 
      return;
    
    active2.toggle();
  

  ekle(Word word) async 
    var val = await WordRepo().add(word);
    showDilog("Kayıt Başarılı");
    update();
    return val;
  

  updateWord(Word word) async 
    var val = await WordRepo().update(word);
    showDilog("Kayıt Başarılı");
    return val;
  

  deleteWord(int? id) async 
    var val = await WordRepo().deleteById(id!);
    return val;
  

  fillWordList() async 
    data.value = await WordRepo().getAll();
  

  translateLanguage(String newValue) async 
    if (newValue == null || newValue.length == 0) 
      return;
    
    List list = ["I", "i"];
    if (newValue.length == 1 && !list.contains(newValue)) 
      return;
    

    var translate = await translator.translate(newValue, from: 'en', to: 'tr');

    controllerInput2.text = translate.toString();
    return translate;
  

  showDilog(String message) 
    Get.defaultDialog(title: "Bilgi", middleText: message);
  

  addNote() async 
    var word =
        Word(wordEn: controllerInput1.text, wordTr: controllerInput2.text);
    await ekle(word);
    fillWordList();
  

  clear() 
    controllerInput2.clear();
    controllerInput1.clear();
  

  updateNote() async 
    var word =
        Word(wordEn: controllerInput1.text, wordTr: controllerInput2.text);
    await updateWord(word);
    await fillWordList();
    update();
  

查看类:Main_page.dart

class MainPage extends StatelessWidget 
  String _firstLanguage = "English";
  String _secondLanguage = "Turkish";

  WordController controller = Get.put(WordController());
  final _formKey = GlobalKey<FormState>();


  @override
  Widget build(BuildContext context) 
    return Scaffold(
      drawer: _drawer,
      backgroundColor: Colors.grey.shade300,
      appBar: _appbar,
      body: _bodyScaffold,
      floatingActionButton: _floattingActionButton,
    );
  

  SingleChildScrollView get _bodyScaffold 
    return SingleChildScrollView(
      child: Column(
        children: [
          chooseLanguage,
          translateTextView,
          //futureBuilder,
        ],
      ),
    );
  

  AppBar get _appbar 
    return AppBar(
      backgroundColor: Colors.blueAccent,
      centerTitle: true,
      title: Text("TRANSLATE"),
      elevation: 0.0,
    );
  

  get chooseLanguage => Container(
        height: Get.height / 12.0,
        decoration: buildBoxDecoration,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            firstChooseLanguage,
            changeLanguageButton,
            secondChooseLanguage,
          ],
        ),
      );

  get buildBoxDecoration 
    return BoxDecoration(
      color: Colors.white,
      border: Border(
        bottom: BorderSide(
          width: 3.5,
          color: Colors.grey,
        ),
      ),
    );
  



  get translateTextView => Column(
        children: [
          Card(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(8.0)),
            ),
            margin: EdgeInsets.only(left: 2.0, right: 2.0, top: 2.0),
            child: _formTextField,
          ),
          Obx(
            () => Container(
              height: controller.active2 == true
                  ? Get.height / 2.3
                  : Get.height / 1.6,
                child: historyWordList,

                

            ),
          )
        ],
      );
  Widget get historyWordList 
    return ListView.builder(
      itemCount: controller.data.length,
      itemBuilder: (context, index) 
        return Card(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(5.0)),
          ),
          margin: EdgeInsets.only(left: 8.0, right: 8.0, top: 0.8),
          child: Container(
            color: Colors.white30,
            padding: EdgeInsets.only(left: 8.0, top: 8.0, bottom: 8.0),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Obx(()=>Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    firstText(controller.data[index].value),
                    secondText(controller.data[index].value),
                  ],
                )),
                historyIconbutton,
              ],
            ),
          ),
        );
      ,
    );
  


  get _formTextField 
    return Form(
      key: _formKey,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            color: Colors.white30,
            height: Get.height / 6.0,
            padding: EdgeInsets.only(left: 16.0, top: 8.0, bottom: 8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                textFormFieldEntr, //one textfield
                favoriIconButton,
              ],
            ),
          ),
          textFormField, //burası kapandı // second
        ],
      ),
    );
  

  get textFormFieldEntr 
    return Flexible(
      child: Container(
        height: Get.height / 5.2,
        child: TextFormField(
          readOnly: true, //sadece okuma
          onTap: () 
            showMaterialBanner();
          ,
          controller: controller.controllerInput1,
          onChanged: (text) 
            if (text.isEmpty) 
              controller.active2(false);
             else 
              controller.active2(true);
            
          ,
          maxLines: 6,
          decoration: InputDecoration(
            hintText: "Enter",
            contentPadding: const EdgeInsets.symmetric(vertical: 5.0),
          ),
        ),
      ),
    );
  

  void showMaterialBanner() 
    ScaffoldMessenger.of(Get.context!).showMaterialBanner(MaterialBanner(
        backgroundColor: Colors.white,
        content: Padding(
          padding: const EdgeInsets.only(top: 40.0),
          child: Column(
            children: [
              TextFormField(
                controller: controller.controllerInput1,
                maxLines: 7,
                onChanged: (text) 
                  controller.translateLanguage(text);
                  if (text.isNotEmpty) 
                    controller.showText();
                  
                  // controller.showText();
                ,
                decoration: InputDecoration(
                  suffixIcon: IconButton(
                    onPressed: () 
                      if (controller.controllerInput1.text.length > 0) 
                        controller.clear();
                       else 
                        controller.clear();
                        closeBanner();
                      
                    ,
                    icon: Icon(Icons.clear),
                  ),
                  contentPadding: const EdgeInsets.symmetric(vertical: 5.0),
                ),
              ),
              SizedBox(height: Get.height / 6.0),
              TextFormField(
                controller: controller.controllerInput2,
                maxLines: 7,
                onTap: () 
                  if (controller.controllerInput1.text.isEmpty) 
                    controller.clear();
                   else 
                    controller.addNote();
                  
                  closeBanner();
                ,
                decoration: InputDecoration(
                  contentPadding: const EdgeInsets.symmetric(vertical: 5.0),
                ),
              ),
            ],
          ),
        ),
        actions: [
          IconButton(
            onPressed: () 
              closeBanner();
            ,
            icon: Icon(null),
          ),
        ]));
  

  void closeBanner() 
    ScaffoldMessenger.of(Get.context!).hideCurrentMaterialBanner();
  

  get textFormField 
    return Obx(() 
      return Container(
        child: Visibility(
          visible: controller.active2.value, //false
          child: Container(
            color: Colors.white30,
            height: Get.height / 5.2,
            padding:
                EdgeInsets.only(left: 16.0, right: 42.0, top: 8.0, bottom: 8.0),
            child: TextFormField(
              readOnly: true,
              controller: controller.controllerInput2,
              maxLines: 6,
              decoration: InputDecoration(
                contentPadding: const EdgeInsets.symmetric(vertical: 5.0),
              ),
            ),
          ),
        ),
      );
    );
  


  IconButton get historyIconbutton 
    return IconButton(
      onPressed: () ,
      icon: Icon(Icons.history),
      iconSize: 30.0,
    );
  

  Text firstText(Word word) 
    return Text(
      "İngilizce: $word.wordEn ?? """,
      style: TextStyle(
        fontWeight: FontWeight.w600,
      ),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );
  
  get changeLanguageButton 
    return Material(
      color: Colors.white,
      child: IconButton(
        icon: Icon(
          Icons.wifi_protected_setup_rounded,
          color: Colors.indigo,
          size: 30.0,
        ),
        onPressed: () ,
      ),
    );
  

  get secondChooseLanguage 
    return Expanded(
      child: Material(
        color: Colors.white,
        child: InkWell(
          onTap: () ,
          child: Center(
            child: Text(
              this._secondLanguage,
              style: TextStyle(
                color: Colors.blue[600],
                fontSize: 22.0,
              ),
            ),
          ),
        ),
      ),
    );
  

  get firstChooseLanguage 
    return Expanded(
      child: Material(
        color: Colors.white,
        child: InkWell(
          onTap: () ,
          child: Center(
            child: Text(
              this._firstLanguage,
              style: TextStyle(
                color: Colors.blue[600],
                fontSize: 22.0,
              ),
            ),
          ),
        ),
      ),
    );
  

  Text secondText(Word word) 
    return Text(
      "Türkçe: $word.wordTr ?? """,
      style: TextStyle(
        fontWeight: FontWeight.w400,
      ),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );
  

  get favoriIconButton 
    return IconButton(
      alignment: Alignment.topRight,
      onPressed: controller.onClickIconButtonFavori,
      icon: Icon(
        Icons.forward,
        color: Colors.blueAccent,
        size: 28.0,
      ),
    );
  

  FloatingActionButton get _floattingActionButton 
    return FloatingActionButton(
      onPressed: () 
        Get.to(WordListPage());
      ,
      child: Icon(
        Icons.app_registration,
        size: 30,
      ),
    );
  

  Drawer get _drawer 
    return Drawer(
      child: ListView(
        // Important: Remove any padding from the ListView.
        padding: EdgeInsets.zero,
        children: <Widget>[
          userAccountsDrawerHeader,
          drawerFavorilerim,
          drawersettings,
          drawerContacts,
        ],
      ),
    );
  

  ListTile get drawerContacts 
    return ListTile(
      leading: Icon(Icons.contacts),
      title: Text("Contact Us"),
      onTap: () 
        Get.back();
      ,
    );
  

  ListTile get drawersettings 
    return ListTile(
      leading: Icon(Icons.settings),
      title: Text("Settings"),
      onTap: () 
        Get.back();
      ,
    );
  

  ListTile get drawerFavorilerim 
    return ListTile(
      leading: Icon(
        Icons.star,
        color: Colors.yellow,
      ),
      title: Text("Favorilerim"),
      onTap: () 
        Get.to(FavoriListPage());
      ,
    );
  

  UserAccountsDrawerHeader get userAccountsDrawerHeader 
    return UserAccountsDrawerHeader(
      accountName: Text("UserName"),
      accountEmail: Text("E-mail"),
      currentAccountPicture: CircleAvatar(
        backgroundColor: Colors.grey,
        child: Text(
          "",
          style: TextStyle(fontSize: 40.0),
        ),
      ),
    );
  

【问题讨论】:

【参考方案1】:

此错误是由于 null 安全性造成的。尝试在您在函数中传递的每个单词实例之后添加!

【讨论】:

它不工作。 你能在我的代码中发送示例代码吗? 你可以尝试转换 RxList 数据 = [].obs;到 RxList 数据 = [].obs; 我收到此错误:在构建 Obx(具有构建器,脏,状态:_ObxState#68558)时引发了以下 NoSuchMethodError:类“Word”没有实例获取器“值”。接收方:“Word”实例尝试调用:值

以上是关于如何解决这个错误? 'List<Word?>' 不是 'val'E/flutter (29035) 的 'List<Word>' 类型的子类型:#0 RxObjectMixin的主要内容,如果未能解决你的问题,请参考以下文章

如何去掉List中的重复内容

word文档中超链接打不开,提示发生意外错误,怎么解决?

type 'List<FlashcardsList>' 不是 type 'List<Back>' 的子类型如何解决这个问题?

如何使用 HTML Agility Pack 修复格式错误的 HTML?

如何解决错误重复sources.list?

Web工程中使用Freemarker导出Word出错,XML文件中使用<#list abc as being></#list> 标记报错,不能识别。