如何解决flutter 2.2 更新代码停止工作并且在某些项目中没有模拟器
我最近格式化了我的笔记本电脑,所有项目都停止了, 在第一类的主要方法中,我有这些错误
class MyApp extends StatefulWidget {
static void setLocale(BuildContext context,Locale locale) {
_MyAppState state = context.findAncestorStateOfType<_MyAppState>();
state.setLocale(locale);
}
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Locale _locale;
错误 1 说错误:类型为“_MyAppState?”的值?不能分配给类型为“_MyAppState”的变量。 (invalid_assignment 在 [AppName] lib\main.dart:35) 错误 2 说错误:必须初始化不可为空的实例字段 '_locale'。 (not_initialized_non_nullable_instance_field at [AppName] lib\main.dart:44)
我不能再使用 null
setState(() {
if (stut != null) buttonState = 0;
});
stut 错误有错误:不可为空的局部变量 'stut' 必须先赋值,然后才能使用。 (not_assigned_potentially_non_nullable_local_variable at [AppName] lib\myWidgets\AddToBasketButton.dart:65),
必填问题和非必填问题 这是我的代码
class SectionButtonClass {
final String title,image;
final int index;
final String phone;
// final Color titleColor;
const SectionButtonClass(
{required this.title,required this.image,this.index,this.phone});
}
所有不需要的变量都有一个错误,请注意,更新后@required我不得不删除@信号。
WillPopScop 的pressBack 方法因错误而停止工作
// ignore: missing_return
Future<bool> _pressBack() {
Navigator.pushReplacement(
context,MaterialPageRoute(
builder: (context) {
return widget.backTo;
},),);
}
错误:主体可能正常完成,导致返回“空”,但返回类型可能是不可为空的类型。 (body_might_complete_normally at [AppName] lib\UI\AboutUsScr.dart:43)
// ignore: missing_return
Future<bool> pressBack() {
Navigator.of(context).pushReplacement(
new MaterialPageRoute(builder: (BuildContext context) => LoginScr()));
}
错误:主体可能正常完成,导致返回“空”,但返回类型可能是不可为空的类型。 (body_might_complete_normally at [AppName] lib\UI\ActivePhoneNoForgetMyPasswordCodeScr.dart:132)
解决方法
如果你打开你的 pubspec.yaml
文件,你可能有这样的东西:
version: 1.0.0+1
environment:
// sdk might be also set to 2.12.0
sdk: ">=2.9.0 <3.0.0"
//rest
无论如何,这个和你的代码的问题是从 Dart v2.9
开始有一个叫做 null-safety
的特性:Read more here
为了解决这个问题,您可以将您的 sdk
降级到低于 2.9.0
的版本,例如 2.7.0
,这可能是更简单的选择,但也不是更好。
为了真正符合最新标准,您需要开始在代码中实现空安全,例如Key key
现在将变为 Key? key
。
更多信息也在这里:https://dart.dev/null-safety
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。