问题描述
我也在 pubspec 中添加了非 nullabe。我不想更改我的 SDK 版本,因为我已经将它用于其他依赖项,如果我更改版本会导致错误。请让我知道如何消除此错误。请让我知道如何解决这个问题,因为我是 Flutter 的新手
错误:
lib/BluetoothScanningDevices.dart:113:40: Error: Null safety features are disabled for this library.
Try removing the package language version or setting the language version to 2.12 or higher.
children: snapshot.data!
^
我的 pubspec.yaml 文件如下所示:
name: epicare
description: Epilepsy monitoring application.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots,like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in Flutter
# build by specifying --build-name and --build-number,respectively.
# In Android,build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS,build-name is used as CFBundleShortVersionString while build-number used as CFBundLeversion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
analyzer:
enable-experiment:
- non-nullable
dependencies:
table_calendar: ^2.2.1
shared_preferences: ^0.5.4+5
permission_handler: ^5.0.0
contacts_service: ^0.3.10
url_launcher: ^5.4.2
Flutter_switch: ^0.2.2
Flutter:
sdk: Flutter
custom_switch: ^0.0.1
charts_Flutter: ^0.10.0
image_picker: ^0.6.7+22
loading_animations: ^2.1.0
Flutter_spinkit: ^4.1.2
cupertino_icons: ^0.1.3
Flutter_launcher_icons: ^0.9.0
Flutter_local_notifications: ^1.4.4+2
focused_menu: ^1.0.0
Fluttertoast: ^4.0.0
contact_picker: ^0.0.2
google_sign_in: ^3.3.0
http: ^0.12.0+4
provider: ^5.0.0
firebase_core: ^0.5.0
firebase_auth: ^0.18.0+1
Flutter_blue: ^0.8.0
bluetooth_enable: ^0.1.1
Flutter_bluetooth_serial: ^0.2.2
dev_dependencies:
Flutter_test:
sdk: Flutter
import 'package:Flutter/cupertino.dart';
import 'package:Flutter/material.dart';
import 'BluetoothConnectBand.dart';
import 'BluetoothConnectedSuccess.dart';
import 'package:Flutter_blue/Flutter_blue.dart';
class BluetoothScanningDevices extends StatefulWidget {
@override
_BluetoothScanningDevicesstate createState() =>
_BluetoothScanningDevicesstate();
}
class _BluetoothScanningDevicesstate extends State<BluetoothScanningDevices> {
FlutterBlue FlutterBlue = FlutterBlue.instance;
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xffE5E0A1),elevation: 0,centerTitle: true,title: Text(
"Connect Band",style: TextStyle(
fontSize: 15.0,color: Colors.black,fontFamily: 'Montserrat',fontWeight: FontWeight.normal,),leading: IconButton(
icon: Icon(
Icons.arrow_back,onpressed: () {
Navigator.push(
context,MaterialPageRoute(
builder: (context) {
return BluetoothConnectBand();
},);
},actions: [
Padding(
padding: EdgeInsets.only(right: 5.0),child: FlatButton(
onpressed: () {},child: Text(
"Search",style: TextStyle(
fontSize: 15.0,)
],body: Column(
crossAxisAlignment: CrossAxisAlignment.start,children: [
Container(
height: size.height * 0.4,width: size.width,color: const Color(0xffE5E0A1),child: Image.asset(
'assets/images/bluetooth.png',)),Container(
width: size.width,padding: EdgeInsets.symmetric(vertical: 20),child: Text(
"Scanning Available Devices...",style: TextStyle(
fontSize: 15.0,fontWeight: FontWeight.w400,textAlign: TextAlign.center,StreamBuilder<List<BluetoothDevice>>(
stream: Stream.periodic(Duration(seconds: 2))
.asyncMap((_) => FlutterBlue.instance.connectedDevices),initialData: [],builder: (c,snapshot) => Column(
children: snapshot.data!
.map((d) => ListTile(
title: Text(d.name),subtitle: Text(d.id.toString()),trailing: StreamBuilder<BluetoothDeviceState>(
stream: d.state,initialData: BluetoothDeviceState.disconnected,builder: (c,snapshot) {
if (snapshot.data ==
BluetoothDeviceState.connected) {
return RaisedButton(
child: Text('OPEN'),onpressed: (){}
);
}
return Text(snapshot.data.toString());
},))
.toList(),],);
}
}
解决方法
在空安全世界中,我们使用“!”表示该变量不能为空,但为了使用这些很酷的功能,dart SDK 版本应该大于 2.12。
错误信息明确指出了问题所在的行号,您无需更改版本或迁移,删除“!”在 BluetoothScanningDevices.dart 文件中第 113 行的 snapshot.data
之后。
我在代码片段中添加了一条注释,其中必须进行更改。
builder: (c,snapshot) => Column(
// Important: remove the exclamation after snapshot.data to fix the error.
children: snapshot.data!
.map((d) => ListTile(
title: Text(d.name),subtitle: Text(d.id.toString()),trailing: StreamBuilder<BluetoothDeviceState>(
stream: d.state,initialData: BluetoothDeviceState.disconnected,builder: (c,snapshot) {
if (snapshot.data ==
BluetoothDeviceState.connected) {
return RaisedButton(
child: Text('OPEN'),onPressed: (){}
);
}
return Text(snapshot.data.toString());
},),))
,
有多种方法可以在遗留代码中使用不可为 null 的包,但它们需要使用 --no-sound-null-safety
调用各种命令。最好按照 https://dart.dev/null-safety/migration-guide 中所示的步骤迁移您的代码。
如何使用不同版本的相同包 像这样使用 ->(环境上面的分析器)
web.php