我遵循了“启用空安全”dart.dev并将我的整个 Flutter 应用程序迁移到空安全。
现在,我正在尝试使用 运行它flutter run,但是,由于以下错误,它无法启动:
Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:
- package:cloud_firestore_web
- package:firebase_core_web
- package:shared_preferences
- package:url_launcher_web
- package:firebase_auth
- package:http
- package:provider
...
For solutions, see https://dart.dev/go/unsound-null-safety
Failed to compile application.
Run Code Online (Sandbox Code Playgroud)
URL 上的指南说我应该“在迁移包之前等待依赖项迁移”,但我现在想使用 NNBD。
我怎样才能做到这一点?
我有以下 Dart 函数,我现在使用空安全:
void calculate({int factor}) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
分析器抱怨说:
参数“factor”因其类型而不能具有“null”值,并且未提供非空默认值。
这也是我StatelessWidget在 Flutter 中的情况:
class Foo extends StatelessWidget {
const Foo({Key key}): super(key: key);
// ...
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
参数 'key' 因其类型而不能具有 'null' 值,并且未提供非空默认值。
我该如何解决这个问题?
我是新手 Flutter
运行简单的 flutter APP 时出现此错误。我无法弄清楚为什么会发生此错误。
错误
Null check operator used on a null value
我在 main.dart 中的代码
import 'package:flutter/material.dart';
import './ui/login.dart';
void main() {
runApp(new MaterialApp(
title: "Login Template",
home: new Login(),
));
}
Run Code Online (Sandbox Code Playgroud)
我在 login.dart 中的代码
import 'package:flutter/material.dart';
class Login extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new LoginState();
}
}
class LoginState extends State<Login> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Login"),
centerTitle: true,
backgroundColor: Colors.blueAccent.shade50,
),
backgroundColor: Colors.blueGrey, …Run Code Online (Sandbox Code Playgroud) 我尝试使用空安全,但它给了我这个错误:
这需要启用“不可为空”语言功能。尝试更新 pubspec.yaml 以将最小 SDK 约束设置为 2.10.0 或更高版本,并运行“pub get”。
我将 Dart SDK 约束从 更改2.7.0为2.10.0,但它仍然显示此错误。
另外,我升级了我的 Dart 和 Flutter SDK:
dart-sdk v2.10.2 是基于您的来源的最新版本。
颤动(通道稳定,1.22.3,...
尝试以下命令时会出现此问题:
flutter pub run build_runner build --delete-conflicting-outputs
错误信息:
[SEVERE] json_serializable:json_serializable on lib/models/shipping_address/shipping_address.dart:
Generator cannot target libraries that have not been migrated to null-safety.
package:deals_and_delivery/models/shipping_address/shipping_address.dart:6:7
?
6 ? class ShippingAddress {
? ^^^^^^^^^^^^^^^
?
[INFO] Running build completed, took 3.6s
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 49ms
[SEVERE] Failed after 3.7s
pub finished with exit code 1
Run Code Online (Sandbox Code Playgroud)
pubspec.yaml:
dependencies:
json_annotation: ^4.0.0
flutter:
sdk: flutter
...
dev_dependencies:
build_runner: ^1.11.5
json_serializable: ^4.0.2
flutter_test:
sdk: flutter
Run Code Online (Sandbox Code Playgroud)
这些是当前的 flutter …
我想实现制作一个带有不同项目的抽屉,所以我正在为 DrawerItems 创建一个单独的文件,并使用构造函数将数据传递给主文件。但是我在“onPressed”函数上收到以下错误:
“不能将参数类型‘Function’分配给参数类型‘void Function()’”
class DrawerItem extends StatelessWidget {
final String text;
final Function onPressed;
const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
@override
Widget build(BuildContext context) {
return FlatButton(
child: Text(
text,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 18.0,
),
),
onPressed: onPressed,
);
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么?
我正在尝试为我的类创建一个“copyWith”方法,它适用于大多数场景。
问题是当我尝试将可为 null 的属性设置为 null 时,因为我的函数无法识别它是否是有意的。
前任。:
class Person {
final String? name;
Person(this.name);
Person copyWith({String? name}) => Person(name ?? this.name);
}
void main() {
final person = Person("Gustavo");
print(person.name); // prints Gustavo
// I want the name property to be nul
final person2 = person.copyWith(name: null);
print(person2.name); // Prints Gustavo
}
Run Code Online (Sandbox Code Playgroud)
有谁知道这种情况的一些解决方法?这真的很困扰我,我不知道如何避免这种情况。
我是颤振新手。我正在尝试开发一个应用程序。
\n我想显示 Firebase 数据库中的人员列表。但是,我收到以下错误。
\n错误 :
\n\n\n无法无条件调用方法“[]”,因为接收者\n可能为“null”。尝试使调用成为有条件的(使用“?.”)或向目标添加空检查(“!”)。
\n
科德拉\xc4\xb1m :
\n`import 'package:calendar/page/mainPage.dart';\nimport 'package:flutter/material.dart';\nimport 'package:cloud_firestore/cloud_firestore.dart';\n\nclass Staff extends StatefulWidget {\n @override\n _StaffState createState() => _StaffState();\n}\n\nclass _StaffState extends State<Staff> {\n \n final _firestore = FirebaseFirestore.instance;\n\n @override\n Widget build(BuildContext context) {\n // ignore: unused_local_variable\n CollectionReference staffRef = _firestore.collection('staff');\n\n return Scaffold(\n appBar: AppBar(\n title: Text("Personel Listesi"),\n backgroundColor: Colors.redAccent[400],\n actions: <Widget>[\n IconButton(\n icon: Icon(Icons.home),\n onPressed: () {\n Navigator.pushAndRemoveUntil(\n context,\n MaterialPageRoute(builder: (_) => MainPage()),\n (route) => true);\n },\n ),\n ],\n ),\n …Run Code Online (Sandbox Code Playgroud) dart firebase flutter google-cloud-firestore dart-null-safety
我收到“可为空表达式”错误。问题出现在以下几行中:
left: isSideBarOpenedAsync.data ? 0 : 0,
right: isSideBarOpenedAsync.data ? 0 : screenWidth - 35,
Run Code Online (Sandbox Code Playgroud)
isSideBarOpenedAsync类型在哪里AsyncSnapshot<bool>
这是我收到的错误
可为空的表达式不能用作条件。
null在将其用作条件之前,请尝试检查该值是否存在。
这是完整的代码供参考
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:solaris/constants.dart';
import 'package:rxdart/rxdart.dart';
class SideBar extends StatefulWidget {
@override
_SideBarState createState() => _SideBarState();
}
class _SideBarState extends State<SideBar> with SingleTickerProviderStateMixin<SideBar>{
late AnimationController _animationController;
late StreamController<bool> isSidebarOpenedStreamController;
late Stream<bool> isSideBarOpenedStream;
late StreamSink<bool> isSideBarOpenedSink;
final _animationDuration = const Duration(milliseconds: 500);
@override
void initState(){
super.initState();
_animationController = AnimationController(vsync: this, duration: _animationDuration);
isSidebarOpenedStreamController = PublishSubject<bool>();
isSideBarOpenedStream …Run Code Online (Sandbox Code Playgroud)