【问题标题】:I get multiple errors from an old version code because of Null-Safety由于 Null-Safety,我从旧版本代码中收到多个错误
【发布时间】:2022-01-12 23:13:28
【问题描述】:

我正在尝试使用 firebase 修改旧的基本购物应用程序,但它的旧版本代码无法准确获得 null 安全性。任何人都可以帮助我。在这里我得到“不可为空的实例字段'_selectedParam'必须被初始化”对于'数据库'我也得到同样的错误。对于“列表”,我得到“启用空安全性时,默认的“列表”构造函数不可用。”错误。

class MyApp extends StatefulWidget {
      MyApp(this.k);
      final int k;
      @override
      _MyAppState createState() => _MyAppState(k);
    }
    
    
    class _MyAppState extends State<MyApp> {
      CollectionReference games= FirebaseFirestore.instance.collection("games");
      _MyAppState(this.k);
      final int k ;
      static Future<Database>   database ;
      var receivePort = ReceivePort();
      Isolate _isolate;
      final String url = "https://www.freetogame.com/api/games";
      List data ;
      static List listpvp= new List(); static List listmmorpg= new List(); static List listmmofps= new List(); static List listshooter = new List();
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
      String _selectedParam;
      int val;
      String task;
      static String _col="mmofps"; //initially

【问题讨论】:

    标签: flutter dart-null-safety


    【解决方案1】:

    了解 null 安全性的主要内容是,flutter/dart 应该能够准确地知道在小部件生命周期的任何时候是否定义了变量。 在您的示例中, _selectParam 被键入为字符串,因此 dart 期望它始终为字符串类型,并被定义。 但是如果你像你一样声明它,它会一直保持为空,直到你给它分配一个字符串。 所以基本上有两种选择:

    • 将变量声明为可为空的字符串,您不必对其进行初始化:String? _selectParam
    • 使用“late”关键字声明变量。 Late 意味着您声明了一个不可为空的变量,但您没有立即对其进行初始化:late String _selectParam

    至于第二个错误,您不能再使用具有 null 安全性的默认 List() 构造函数,它已被弃用。您可以只使用其他 List 构造函数。例如 List.generate() 或 List.of()。 在你的情况下,你可以这样做: List listmmorpg = List.of([])

    请参考 Dart 官方文档,看看哪些构造函数最适合你。

    【讨论】:

    • 谢谢你,我明白了,但是对于 _MyAppState 我能做什么?
    • 您在使用 _MyAppState 时遇到了哪些错误?
    • "非空实例字段 '_selectedParam' 必须初始化"
    • 如上所述,您必须将String _selectedParam 替换为String? _selectedParam
    • 据我所知它不是字符串,我也尝试添加这个“?”但我不知道它是否在错误的地方它没有工作。
    【解决方案2】:
    class MyApp extends StatefulWidget {
          MyApp(this.k);
          int? k;
          @override
          _MyAppState createState() => _MyAppState(k);
        }
        
        
        class _MyAppState extends State<MyApp> {
          CollectionReference games= FirebaseFirestore.instance.collection("games");
          _MyAppState(this.k);
           int? k ;
          static Future<Database>   database ;
          var receivePort = ReceivePort();
          Isolate? _isolate;
           String? url = "https://www.freetogame.com/api/games";
          List? data ;
          static List listpvp= new List(); static List listmmorpg= new List(); static List listmmofps= new List(); static List listshooter = new List();
          FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
          String? _selectedParam;
          int? val;
          String? task;
          static String _col="mmofps"; //initially
    

    【讨论】:

    • 请避免仅使用代码回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 2018-03-27
    • 2019-06-24
    相关资源
    最近更新 更多