Flutter mobx mobx_codegen 为计算的 getter 生成空安全代码
Flutter mobx mobx_codegen generates null safety code for computed getters
将 mobx 和 mobx_codegen 用于 Flutter。
我的代码 NOT null safe (sdk: ">=2.7.0 <3.0.0").
当 mobx_codegen 为 @computed getters 生成代码时,它使用空安全,不会编译...
示例 class 计数器(查看@computed):
import 'package:mobx/mobx.dart';
// Include generated file
part 'counter.g.dart';
// This is the class used by rest of your codebase
class Counter = _Counter with _$Counter;
// The store-class
abstract class _Counter with Store {
@observable
int value = 0;
@computed
int get test => 0;
@action
void increment() {
value++;
}
}
相关生成代码(counter.g.dart):
Computed<int>? _$testComputed;
显示此错误:
这需要启用 'non-nullable' 语言功能。
尝试更新您的 pubspec.yaml 以将最低 SDK 限制设置为 2.12.0 或更高版本,并 运行 'pub get'.
我是 mobx 的新手,我做错了什么吗?
最近怎么样?
我在我的项目中遇到了同样的问题。我安装了新的 flutter 版本,之后我的项目停止编译。我改变了我对 mobx、flutter_mobx、mobx_codegen 和 build_runner 的依赖,它们起作用了。
我的第一个依赖:
mobx: ^1.2.1+4
flutter_mobx: ^1.1.0+2
build_runner: ^1.11.5
mobx_codegen: ^1.1.2
我最后的依靠:
mobx: ^2.0.6+1
flutter_mobx: ^2.0.4
build_runner: ^2.1.7
mobx_codegen: ^2.0.5+2
从那里,它开始了我的“@computed”问题,而我 运行 build_runner。
我更改了项目的 Dart SDK,它停止显示“@computed”错误。但是破坏了所有源代码,因为我用“非null-safety”开发了我的代码。
所以,正在寻找回复。我发现根本原因是 mobx_codegen 较新的依赖项,它使用空安全并且没有验证我们的 Dart SDK 的方法。
在我看来,它应该在 build_runner 的 运行 期间得到验证。
那么,有人知道我该如何解决这个问题吗?
我有一个应用程序的商业解决方案,我计划在 3 个月内发布。
我的pubspec.yaml是这样的:
name: XPTO
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter 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"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
mobx: ^2.0.6+1
flutter_mobx: ^2.0.4
get_it: ^7.2.0
flutter_search_bar: ^3.0.0-dev.1
carousel_slider: ^4.0.0
intl: ^0.17.0
image_picker: ^0.8.4+5
image_cropper: ^1.4.1
parse_server_sdk_flutter: ^3.1.0
cpf_cnpj_validator: ^2.0.0
brasil_fields: ^1.3.0
mask_text_input_formatter: ^2.1.0
badges: ^2.0.2
flutter_rating_bar: ^4.0.0
expandable: ^5.0.1
flutter_easyloading: ^3.0.3
internet_connection_checker: ^0.0.1+3
cached_network_image: ^3.2.0
flutter_keyboard_visibility: ^5.1.1
json_annotation: ^4.4.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.1.7
mobx_codegen: ^2.0.5+2
graphs: ^2.1.0
json_serializable: ^6.1.4
dependency_overrides:
plugin_platform_interface: ^2.0.0
build_resolvers: ^2.0.6
web_socket_channel: ^2.0.0
analyzer: ^3.2.0
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
- images/logo.jpeg
- images/empty.jpg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
由于您使用的 MobX 版本具有 null-safety,您可以:
- 将您的代码迁移到 null-safety (推荐)。
- 将您的
mobx
、mobx_codegen
和 build_runner
软件包降级到没有 null-safety. 的版本
要迁移您的代码,您可以按照此 Migration Guide。
如果您选择降级,请注意您可能 运行 陷入版本冲突,并且有可能您也必须降级您的 Flutter SDK。
将 mobx 和 mobx_codegen 用于 Flutter。
我的代码 NOT null safe (sdk: ">=2.7.0 <3.0.0").
当 mobx_codegen 为 @computed getters 生成代码时,它使用空安全,不会编译...
示例 class 计数器(查看@computed):
import 'package:mobx/mobx.dart';
// Include generated file
part 'counter.g.dart';
// This is the class used by rest of your codebase
class Counter = _Counter with _$Counter;
// The store-class
abstract class _Counter with Store {
@observable
int value = 0;
@computed
int get test => 0;
@action
void increment() {
value++;
}
}
相关生成代码(counter.g.dart):
Computed<int>? _$testComputed;
显示此错误: 这需要启用 'non-nullable' 语言功能。 尝试更新您的 pubspec.yaml 以将最低 SDK 限制设置为 2.12.0 或更高版本,并 运行 'pub get'.
我是 mobx 的新手,我做错了什么吗?
最近怎么样?
我在我的项目中遇到了同样的问题。我安装了新的 flutter 版本,之后我的项目停止编译。我改变了我对 mobx、flutter_mobx、mobx_codegen 和 build_runner 的依赖,它们起作用了。
我的第一个依赖:
mobx: ^1.2.1+4
flutter_mobx: ^1.1.0+2
build_runner: ^1.11.5
mobx_codegen: ^1.1.2
我最后的依靠:
mobx: ^2.0.6+1
flutter_mobx: ^2.0.4
build_runner: ^2.1.7
mobx_codegen: ^2.0.5+2
从那里,它开始了我的“@computed”问题,而我 运行 build_runner。
我更改了项目的 Dart SDK,它停止显示“@computed”错误。但是破坏了所有源代码,因为我用“非null-safety”开发了我的代码。
所以,正在寻找回复。我发现根本原因是 mobx_codegen 较新的依赖项,它使用空安全并且没有验证我们的 Dart SDK 的方法。
在我看来,它应该在 build_runner 的 运行 期间得到验证。
那么,有人知道我该如何解决这个问题吗?
我有一个应用程序的商业解决方案,我计划在 3 个月内发布。
我的pubspec.yaml是这样的:
name: XPTO
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter 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"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
mobx: ^2.0.6+1
flutter_mobx: ^2.0.4
get_it: ^7.2.0
flutter_search_bar: ^3.0.0-dev.1
carousel_slider: ^4.0.0
intl: ^0.17.0
image_picker: ^0.8.4+5
image_cropper: ^1.4.1
parse_server_sdk_flutter: ^3.1.0
cpf_cnpj_validator: ^2.0.0
brasil_fields: ^1.3.0
mask_text_input_formatter: ^2.1.0
badges: ^2.0.2
flutter_rating_bar: ^4.0.0
expandable: ^5.0.1
flutter_easyloading: ^3.0.3
internet_connection_checker: ^0.0.1+3
cached_network_image: ^3.2.0
flutter_keyboard_visibility: ^5.1.1
json_annotation: ^4.4.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.1.7
mobx_codegen: ^2.0.5+2
graphs: ^2.1.0
json_serializable: ^6.1.4
dependency_overrides:
plugin_platform_interface: ^2.0.0
build_resolvers: ^2.0.6
web_socket_channel: ^2.0.0
analyzer: ^3.2.0
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
- images/logo.jpeg
- images/empty.jpg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
由于您使用的 MobX 版本具有 null-safety,您可以:
- 将您的代码迁移到 null-safety (推荐)。
- 将您的
mobx
、mobx_codegen
和build_runner
软件包降级到没有 null-safety. 的版本
要迁移您的代码,您可以按照此 Migration Guide。
如果您选择降级,请注意您可能 运行 陷入版本冲突,并且有可能您也必须降级您的 Flutter SDK。