相关疑难解决方法(0)

迁移到 Dart 空安全后,“无法无条件调用运算符,因为接收器可以为空”错误

我正在升级一个基于 Flutter 框架的个人包。我在 Flutter Text 小部件源代码中注意到这里有一个空检查:

if (textSpan != null) {
  properties.add(textSpan!.toDiagnosticsNode(name: 'textSpan', style: DiagnosticsTreeStyle.transition));
}
Run Code Online (Sandbox Code Playgroud)

但是,textSpan!仍然使用!运算符。不textSpan应该在不必使用!运算符的情况下提升为不可为空的类型吗?但是,尝试删除运算符会出现以下错误:

An expression whose value can be 'null' must be null-checked before it can be dereferenced.
Try checking that the value isn't 'null' before dereferencing it.
Run Code Online (Sandbox Code Playgroud)

这是一个独立的示例:

An expression whose value can be 'null' must be null-checked before it can be dereferenced.
Try checking that the value isn't 'null' before dereferencing it.
Run Code Online (Sandbox Code Playgroud)

我收到一个编译时错误:

错误:“字符串?”类型的值 …

dart type-promotion flutter dart-null-safety

18
推荐指数
2
解决办法
1万
查看次数

如何在 dart 中安全地解开可选变量?

我找不到像我们在 swift 中那样安全地解开可选变量的方法

var myString: String?
if let myString = myString {
  print(myString) // myString is a string
}
Run Code Online (Sandbox Code Playgroud)

或者在科特林中

var myString: String?
if (myString != null) {
   print(myString) // myString is not null
}

// or
myString?.let {
   print(it) // myString is not null
}
Run Code Online (Sandbox Code Playgroud)

在 Dart 中,我必须执行以下操作,这看起来不太好:

String? myString;
if (myString != null) {
   print(myString); // myString still an optional
   print(myString!); // myString is now a String! (because of the force unwrap)
}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以像其他空安全语言一样以干净的方式安全地解包?或者我们必须始终在空检查后强制解包变量?

dart

13
推荐指数
1
解决办法
1万
查看次数

不可为空的变量 '_database' 必须被初始化

我正在使用 flutter 制作 Windows 应用程序,在使用 sqflite 制作数据库时,会弹出此错误,我不知道如何真正解决此问题。

import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class DatabaseHelper {
  static final _dbName = 'Database.db';
  static final _dbVersion = 1;
  static final _tableName = 'my table';

  static final columnId = '_id';
  static final columnName = 'name';

  DatabaseHelper._privateConstuctor();
  static final DatabaseHelper instance = DatabaseHelper._privateConstuctor();

  static Database _database;
  Future<Database> get database async {
    if (_database == null) {
      _database = await _initiateDatabase();
    }
    return _database;
  }

  _initiateDatabase() async {
    Directory directory = await getApplicationDocumentsDirectory(); …
Run Code Online (Sandbox Code Playgroud)

sql database dart flutter sqflite

5
推荐指数
1
解决办法
4547
查看次数

尽管存在空检查,但 Dart 空安全性会抱怨函数变为空

所以我有一个以下类,它onPressed作为一个可以为空的道具:

class Class1 {
  final Function? onPressed;
  
  const Class1({ this.onPressed });
  
  callMethod() {
    if(onPressed != null) {
      onPressed(); // Complains here
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我检查了是否onPressed为空。但是 dart 仍然抱怨这条消息The function can't be unconditionally invoked because it can be 'null'. (view docs) Try adding a null check ('!').请帮助我。

dart flutter

4
推荐指数
1
解决办法
1316
查看次数

具有空安全性的 Flutter 条件渲染

如果过期不为空,我想渲染文本小部件。所以我检查了 null

但出现错误

在此输入图像描述

class TileButton extends StatelessWidget {
  final DateTime? expires;

  const TileButton({
    Key? key,
    this.expires,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Text(
          'Hello Flutter',
        ),
        if (expires != null)
          Text(
            expires.day.toString(),
          ),
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

dart flutter dart-null-safety

1
推荐指数
1
解决办法
7172
查看次数