我正在使用的进口:
import 'package:flutter/material.dart';
import 'package:splashscreen/splashscreen.dart';
import 'package:imagetotext/homePage.dart';
Run Code Online (Sandbox Code Playgroud)
然而,linter 给了我以下警告:
The library 'package:splashscreen/splashscreen.dart'' is legacy, and should not be imported into a null safe library. Try migrating the imported library. import_of_legacy_library_into_null_safe
Run Code Online (Sandbox Code Playgroud)
我正在使用的依赖项:
cupertino_icons: ^1.0.3
splashscreen: ^1.3.3
Run Code Online (Sandbox Code Playgroud)
我搜索了很长一段时间,没有在 SO 或其他网站上找到解决此问题的明确解决方案。
我有一个颤振测试:
test('Create Repo and Read JSON', () {
Repository repository = CreateRepository();
...
}
Run Code Online (Sandbox Code Playgroud)
CreateRepository()最终调用一个方法,代码如下:
var jsonString = await rootBundle.loadString(vendorDataFilePath);
Run Code Online (Sandbox Code Playgroud)
这会导致错误:Null check operator used on a null value
我执行的代码均未使用空检查运算符 ( !),那么此错误来自何处以及如何修复它?
我在这段代码中收到警告:
Future<int?> foo() async {
if (someCondition) return 42;
}
Run Code Online (Sandbox Code Playgroud)
该函数具有可为空的返回类型“FutureOr<int?>”,但结束时不返回值。
我告诉 Dart Future 可能会以可空类型完成int?,那么为什么我必须显式返回null才能摆脱警告呢?
我试图制作一个谷歌驱动器应用程序,列出驱动器中的文件。但我在空值错误上使用了空检查运算符。我知道发生了什么事。但我无法解决它。
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextButton(
onPressed: () {},
child: Text('UPLOAD'),
),
if (list != null)
SizedBox(
height: 300,
width: double.infinity,
child: ListView.builder(
shrinkWrap: true,
itemCount: list!.files?.length,
itemBuilder: (context, index) {
final title = list!.files![index].originalFilename;
return ListTile(
leading: Text(title!),
trailing: ElevatedButton(
child: Text('Download'),
onPressed: () {
},
),
);
},
),
)
],
),
),
floatingActionButton: Row(
children: [
FloatingActionButton(
onPressed: _listGoogleDriveFiles,
child: Icon(Icons.photo),
),
FloatingActionButton(
onPressed: _incrementCounter,
tooltip: …Run Code Online (Sandbox Code Playgroud) 我想使用 FirebaseAuth.instance (非常量值)作为构造函数中的默认参数。
我有这门课:
class MailAuth {
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
MailAuth();
// methods using firebaseAuth
}
Run Code Online (Sandbox Code Playgroud)
现在我想对这个类进行单元测试,所以我想模拟firebaseAuth并将模拟注入到构造函数中。它应该是一个命名参数,默认值为FirebaseAuth.instance。我尝试过这样的事情:
class MailAuth {
final FirebaseAuth firebaseAuth;
MailAuth({this.firebaseAuth = FirebaseAuth.instance});
// methods using firebaseAuth
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它FirebaseAuth.instance不是一个常数。
我怎样才能实现这种行为?
我有一个可能包含空值的字符串列表,如何对该列表进行排序?
我正在使用Flutter beta 通道,因此会检查空安全性。这是我到目前为止所拥有的:
List<String?> _list = [
'beta',
'fotxtrot',
'alpha',
null,
null,
'delta',
];
_list.sort((a, b) => a!.compareTo(b!));
Run Code Online (Sandbox Code Playgroud)
我如何得到这个结果:
_list = [
null,
null,
'alpha',
'beta',
'delta',
'fotxtrot',
];
Run Code Online (Sandbox Code Playgroud) 最近 Flutter 团队宣布 Dart 语言现在支持声音空安全!并且在 Dart 2.12 和 Flutter 2 中可用。我想将应用程序代码迁移到空安全。
如何将我的包迁移到空安全,
如果有人可以提供有关迁移到 null-safety 的说明,那就太好了。
虽然我知道这可能看起来像一个重复,有多少关于这个错误的不同帖子,但我收到这个错误(我认为)在四个不同的屏幕上是不同的原因。
我的应用程序启动了,但是每当我尝试导航我的应用程序时,我最终都会收到红屏错误,因为它说屏幕内的一个小部件在空值上使用了 Null Check 运算符。
以下是我得到的错误:
以下 _CastError 在构建 NotificationBanner(dirty): Null 检查运算符时被抛出,用于空值。相关的导致错误的小部件是: NotificationBanner file:///Users/alexm/carebloom-flutter/lib/Views/homeview.dart:9:7
在构建 AlertBanner(dirty): Null check operator 时抛出了以下 _CastError 用于空值。相关的导致错误的小部件是:AlertBanner file:///Users/alexm/carebloom-flutter/lib/Views/alerts_view.dart:57:15
构建 MessageOverView(dirty): Null check operator used on a null value 时抛出了以下 _CastError。相关的导致错误的小部件是:MessageOverView file:///Users/alexm/carebloom-flutter/lib/widgets/navbar.dart:41:16
这个有点不同
以下 _CastError 被抛出构建 SettingsView(dirty, dependencies: [_EffectiveTickerMode], state: _SettingsViewState#ddb1e(tickers: tracking 1 ticker)): Null check operator used on a null value。相关的导致错误的小部件是:SettingsView file:///Users/alexm/carebloom-flutter/lib/widgets/navbar.dart:49:16
现在我假设错误位于作为 WidgetName(dirty) 返回的任何小部件中。所以每一个的代码都在下面。但是,除此之外,我还想提出一些问题,以便更好地理解并帮助我将来诊断问题。当它有 WidgetName(dirty) 时,这究竟意味着什么(dity)?除了显示特定小部件的错误消息外,它还给出了“相关的导致错误的小部件路径,这是我应该查看的路径吗?”
通知横幅:
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('alerts')
.where('access',
arrayContains: FirebaseAuth.instance.currentUser!.uid)
.where('read', isEqualTo: false)
//.orderBy('createdAt', …Run Code Online (Sandbox Code Playgroud) 在大于 2.12.0 的 dart sdk 中,我们经常使用“?” 确保该参数也可以具有空值,但是如何使用 ? 数据类型没有?或相反亦然。什么可以是最喜欢的方式来整理“int”?不能是“int”类型。
将flutter项目升级到null-safety时出现如下错误
The name 'FutureOr' isn't a type, so it can't be used in an 'as' expression.
Try changing the name to the name of an existing type, or creating a type with the name 'FutureOr'
Run Code Online (Sandbox Code Playgroud)