【问题标题】:Error in class Constructor in Flutter(VS Code)Flutter 中的类构造函数错误(VS Code)
【发布时间】:2021-10-22 10:32:49
【问题描述】:

我是 Flutter 的新手,所以我正在试验它,我尝试在 vs 代码中创建一个自定义类和该类的构造函数,我不断收到关于类构造函数的错误消息。错误如下所示“必须初始化不可为空的实例字段作者(这是类的属性之一的名称)idk这是什么错误我已经尝试了各种在线资源但只是徒劳无功。看看代码和识别错误的帮助将非常有帮助。

代码:

import 'package:flutter/material.dart';
import 'quote_class.dart';

void main() {
  runApp(const MaterialApp(
    home: Home(),
    debugShowCheckedModeBanner: false,
  ));
}

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  List<Quotes> quotes = [
    Quotes(
         "Be yoursefl cause everyone else is taken","Oscar Wilde"),
    Quotes(
      "I have nothing to declare except my genius",  "Einstien"),
    Quotes(
        "The truth is rarely pure and never simple", "Priyanka"),
    Quotes(
          "Trust those who are still in the search of truth and not those who has already found one","Hey")
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.blue[400],
        appBar: AppBar(
          title: const Text("Awesome Quotes"),
          centerTitle: true,
          backgroundColor: Colors.purple[200],
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: quotes
              .map((quote) => Text('${quote.text} - ${quote.author}'))
              .toList(),
        ));
  }
}

出现错误的自定义类的代码,我正在尝试使用上述 main.dart 文件中的自定义类


class Quotes {
  String text;
  String author;

  Quotes(String text, String author) {
    this.text = text;
    this.author = author;
  }
}



错误信息:

lib/quote.dart:5:16: Error: The parameter 'text' can't have a value of 'null' because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
  Quotes({this.text, this.author});
               ^^^^
lib/quote.dart:5:27: Error: The parameter 'author' can't have a value of 'null' because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
  Quotes({this.text, this.author});
                          ^^^^^^

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以在 Quotes 模型中使用 null 安全性,只需在属性名称前放一个问号

    import 'package:flutter/material.dart';
    
    
    void main() {
      runApp(const MaterialApp(
        home: Home(),
        debugShowCheckedModeBanner: false,
      ));
    }
    
    class Home extends StatefulWidget {
      const Home({Key? key}) : super(key: key);
    
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> {
      List<Quotes> quotes = [
        Quotes(
            "Be yoursefl cause everyone else is taken","Oscar Wilde"),
        Quotes(
            "I have nothing to declare except my genius",  "Einstien"),
        Quotes(
            "The truth is rarely pure and never simple", "Priyanka"),
        Quotes(
            "Trust those who are still in the search of truth and not those who has already found one","Hey")
      ];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            backgroundColor: Colors.blue[400],
            appBar: AppBar(
              title: const Text("Awesome Quotes"),
              centerTitle: true,
              backgroundColor: Colors.purple[200],
            ),
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: quotes
                  .map((quote) => Text('${quote.text} - ${quote.author}'))
                  .toList(),
            ));
      }
    }
    
    class Quotes {
      String? text;
      String? author;
    
      Quotes(String text, String author) {
        this.text = text;
        this.author = author;
      }
    }
    

    输出:

    【讨论】:

    • 自定义类文件在我执行此操作后没有显示任何错误,但是当我运行它时会弹出相同的错误。我只需要用问号对自定义类文件进行更改吗?
    • 但我自己没有收到任何错误,请更新我的答案或查看此网址dartpad.dartlang.org/…
    • 错误表明位置参数太少。我不知道可能是什么原因。你是在 vs code 上运行的吗?还是只有 vs 代码显示我不知道的错误
    • 请从dartpaddartpad.dartlang.org/…签出
    【解决方案2】:

    不考虑构造函数的body。太多的混乱,太多的机会或太难以确定。在 dart 中,你有一种方法可以直接告诉编译器你想要参数去哪里,你不必在正文中把它拼出来。

    这是你想要的类:

    class Quotes {
      final String text;
      final String author;
    
      const Quotes(this.text, this.author);
    }
    

    【讨论】:

      【解决方案3】:

      您的项目似乎正在运行,并启用了健全的 null 安全性。

      所以,对于你的班级

      class Quotes {
      String text;
      String author;
      
       Quotes(String text, String author) {
         this.text = text;
         this.author = author;
       }
      }
      

      字段 text 和 author 不能为空(将它们声明为带有问号的字符串?将使它们可以为空)并可能修复错误。但是,如果您希望它们不可为空,则将它们声明为您拥有的并在其前面添加一个“late”关键字,这将使您的文本/作者值在准备好之前为空。

      类如下

      class Quotes {
      late String text;
      late String author;
      
      Quotes(String text,String author) {
        this.text = text;
        this.author = author;
       } 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 2016-07-11
        • 1970-01-01
        • 1970-01-01
        • 2018-08-25
        • 1970-01-01
        • 2018-06-20
        相关资源
        最近更新 更多