'Flutter mobx mobx_codegen generates null safety code for computed getters

Using mobx and mobx_codegen for Flutter.

My code is NOT null safe (sdk: ">=2.7.0 <3.0.0").

When mobx_codegen generates code for @computed getters, it uses null safety, which won't compile...

Sample class counter (look at @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++;
  }
}

Relevant generated code (counter.g.dart):

Computed<int>? _$testComputed;

Shows this error: This requires the 'non-nullable' language feature to be enabled. Try updating your pubspec.yaml to set the minimum SDK constraint to 2.12.0 or higher, and running 'pub get'.

I'm new to mobx, am I doing something wrong?



Solution 1:[1]

How´s it going?

I am with the same issue in my project. I installed the new flutter version and after that my project stopped to compile. I changed my dependences of mobx, flutter_mobx, mobx_codegen and build_runner and them worked.

My first dependences:

  mobx: ^1.2.1+4
  flutter_mobx: ^1.1.0+2
  build_runner: ^1.11.5
  mobx_codegen: ^1.1.2

My last dependences:

  mobx: ^2.0.6+1
  flutter_mobx: ^2.0.4
  build_runner: ^2.1.7
  mobx_codegen: ^2.0.5+2

From there, It has started my issue of "@computed" while I run the build_runner.

I changed the Dart SDK of my project and It stopped to show error of "@computed". But broke all source code because I developed my code with "non- null-safety".

So, looking for replies. I figured out the root cause is the specifically the mobx_codegen newer dependency that uses null safety and doesn´t have a tratament to valid our Dart SDK.

In my opnion, It should´ve validated while running of build_runner.

So, Someone knows how could I resolve that issue?

I have a commercial solution of app, and I´ve planned to release until 3 months.

my pubspec.yaml is so:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luiz Gustavo Oliveira Maciel