Mobx.dart: observable list isn't a valid override of

Created on 2 Jan 2020  ·  42Comments  ·  Source: mobxjs/mobx.dart

After update the mobx version I'm having issue with all of my Stores that have a observable list. Lets say that I have the following code:

class EventsStore = _EventsStore with _$EventsStore;

abstract class _EventsStore with Store {

  final EventsService eventsService;

  _EventsStore(this.eventsService) : assert(eventsService != null);

//...

  @observable
  List<Event> _events = <Event>[];

//...
}

I'm receiving a error saying:

error: '_$EventsStore._events' ('dynamic Function()') isn't a valid override of '_EventsStore._events' ('List<Event> Function()').

If I remve the _events property and run flutter run build_runner again the error disappear and it goes back to normal

bug

Most helpful comment

Hi, I think this issue persist if I try to create a ObservableList of a generated model class from MOOR library.

'LocalItem' is the name of my generated class by moor library, and mobx code gen, generates the ObservableList type as dynamic.

I am using the lastest version of both mobx and mobx code gen. Is there any work around for this?

All 42 comments

@shyndman ?

You can see more on the image bellow

image

The fix is in this PR: #368. Will be merged once reviewed.

Please try with the latest version of mobx_codegen: 0.3.13

Thank you. I will update the packages again and test it

@pavanpodila it still the same after updating mobx_codegen to 0.3.13

UPDATE
When I updated mobx_codegen it also forced me to update mobx to 0.4.0

@pedromassango, is it still an issue after upgrading both?

Yes @shyndman

Wonderful. Closing this off.

@shyndman I mean I still receiving the same error after update mobx_codegen version

Sorry about that. Can you post a some code? I'd like to get a reproduction going locally.

Give me a minute, I will reproduce it

Here is my code with the latest packages version

part 'events_store.g.dart';

class EventsStore = _EventsStore with _$EventsStore;

abstract class _EventsStore with Store {

  final EventsService eventsService;

  _EventsStore(this.eventsService) : assert(eventsService != null);

 //...

  @observable
  List<Event> _events = [];

  @computed
  List<Event> get movies =>
      _events.where((event) => event.type == EventType.movie).toList();

  @computed
  List<Event> get parties =>
      _events.where((event) => event.type == EventType.party).toList();

  @computed
  bool get hasData => _events.isNotEmpty;

//...
}

I was hoping for some completely self-contained, so that I can copy and paste into an empty project to see the problem.

But let me give this a go.

Hm, no problem on my end, generating from this code: https://gist.github.com/shyndman/cb08bd1910de3043c5270ee781d2cd27

Let's try to figure out what's different. In your project's pubspec.lock, you should see an entry for the analyzer package. It might look something like this:

analyzer:
    dependency: transitive
    description:
      name: analyzer
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.39.2+1"

Can you copy and paste yours?

I noticed this today too, the exact same issue. The mixin class is generating a dynamic getter and setter for collection types 😢.

Do check the analyzer version, it should be as @shyndman pointed out.

Right.

In the meantime, I'm going to downgrade my local analyzer dependency to see if I can break things.

@shyndman I think you should downgrade to the same version that @pedromassango has. That would be make it more reliable.

True, but I got a solid repro at the minimum version. It's the same issue.

For future: We should work out an automated testing strategy to use multiple versions if we plan on shipping with ranges.

Here is my pubspec.lock

  analyzer:
    dependency: transitive
    description:
      name: analyzer
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.37.1+1"

Great!

OK, so quick fix is to upgrade your analyzer version, if it's possible with your dependencies.

I'm working on a more comprehensive fix right now.

As @Aidanvii7 said if you open the generated g.dart class the List property as the dynamic type!

For future: We should work out an automated testing strategy to use multiple versions if we plan on shipping with ranges.

I think there is a way to create a matrix test on CircleCI. Will have to explore.

So my analyser version was 0.36.4, which was caused by functional_widget, so removing my dependency to functional_widget solved this.

cc @rrousselGit

@pavanpodila Whatever the problem is, it's fixed by analyzer 0.38.5+.

The issue (prior to 0.38.5) doesn't seem to be directly related to the logic of our code — the type of the VariableElement coming into visitFieldDeclaration is actually dynamic when it should be List<T>...which is very odd, because there's no way an analyzer recognizes List<T> that way in the common case. So then, is it some strange combination of dependencies? build_resolvers related?

Accessing the analysis errors for the file show that Stream and Future are unrecognized as well.

Any thoughts @rrousselGit?

Great detective work @shyndman 👍. I remember there was an issue with the build_resolver not detecting certain types like dart:ui, possibly because of the server Dart SDK. Although not related, I think there might be some code path where the resolver is failing to detect generic types correctly.

That said, I think we will have to bump up the min-version of analyzer to 0.38.5 to avoid any further issues ?

Alright, then an upgrade it is. I just wish I understood better. I looked at each of the fixed bugs listed in analyzer's changelog, but nothing stood out to me as being relevant. I'm going to do one more pass, then I'll send a PR.

We ended up increasing the minimum version requirement for analyzer to 0.38.5, and pushed a mobx_codegen 0.4.0 with that change.

Hi, I still have the problem with those versions :

Flutter 1.12.13+hotfix.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 27321ebbad (5 weeks ago) • 2019-12-10 18:15:01 -0800
Engine • revision 2994f7e1e6
Tools • Dart 2.7.0
analyzer:
    dependency: transitive
    description:
      name: analyzer
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.38.5"



md5-c502ac21193c47e90fbead3d190d9efb



mobx: ^0.4.0+1
flutter_mobx: ^0.3.6
mobx_codegen: ^0.4.0+1

@pedromassango, did the fix worked for you ? Or should this issue still be open ?

Hi @mfarizon,

Can you post a snippet that fails?

@mfarizon I didn't tested it yet

Hi @shyndman , it took me some hours yesterday, but I found what was causing my store.g.dart not be generated correctly.
I was using the keyword as in an import, and it was working fine with mobx: ^0.3.6 and mobx_codegen: ^0.3.11, and it's now (with mobx 0.4.0+1 and mobx_codegen 0.4.0+1) throwing the error ObservableList.

Here is a minimal Store to reproduce :

import 'package:mobx/mobx.dart';
import 'dart:math' as Math;
//import 'dart:math' show Rectangle;

part 'main_store.g.dart';

class MainStore = _MainStore with _$MainStore;

abstract class _MainStore with Store {
  @observable
  ObservableList<String> observableList = ObservableList();
}

Apparently the keyword show is not affecting the generation and as does.

@shyndman, I thought we had fixed this with import aliases ?

You're right, just found this closed issue : https://github.com/mobxjs/mobx.dart/issues/130
So I guess it's a regression.

Yup, definitely should have been fixed. Let me get a repro going.

OK, I fixed that problem, and a bunch of related ones in #399

Regarding build_resolvers, I'm going to be sending a PR to fix issue in the next couple of days.

Hi, I think this issue persist if I try to create a ObservableList of a generated model class from MOOR library.

'LocalItem' is the name of my generated class by moor library, and mobx code gen, generates the ObservableList type as dynamic.

I am using the lastest version of both mobx and mobx code gen. Is there any work around for this?

I had the same issue as blinkn, but managed to get it working by using Moor's builder options, dividing up the build into steps, so that files generated by Moor in the first step, can be used in other builders in the next step(s).
The information can be found on the builder_options page in the Moor documentation.
In my case I had to:

  • add a build.yaml file (same dir as the pubspec.yaml)
  • add the code suggested on the page to that file.
  • rename run_built_value to run_mobx (although I don't think this matters)
  • add my package name to the depencendies part of the run_mobx step.
  • rename built_value_generator|built_value in the first step to: mobx_codegen|mobx_generator
  • Change all files using Moor generated code from filename.g.dart to filename.moor.dart
  • regenerate code.

I had the same issue as blinkn, but managed to get it working by using Moor's builder options, dividing up the build into steps, so that files generated by Moor in the first step, can be used in other builders in the next step(s).
The information can be found on the builder_options page in the Moor documentation.
In my case I had to:

  • add a build.yaml file (same dir as the pubspec.yaml)
  • add the code suggested on the page to that file.
  • rename run_built_value to run_mobx (although I don't think this matters)
  • add my package name to the depencendies part of the run_mobx step.
  • rename built_value_generator|built_value in the first step to: mobx_codegen|mobx_generator
  • Change all files using Moor generated code from filename.g.dart to filename.moor.dart
  • regenerate code.

Followed your instructions but it seems that is not working for me. The *.moor.dart files were generated, but my mobx stores are still trying to use dynamic rather moor generated classes to my observables =/

my build.yaml file is the following:

targets:
    $default:
      builders:
        # disable the default generator and enable the one emitting a .moor.dart file
        moor_generator:
          enabled: false
        moor_generator|moor_generator_not_shared:
          enabled: true
          # If needed, you can configure the builder like this:
          # options:
          #   skip_verification_code: true
          #   use_experimental_inference: true

        # Run mobx_generator when moor is done, which is not in this target.
        mobx_codegen|mobx_generator:
          enabled: false
        # all other builders that need to work on moor classes should be disabled here
        # as well

    run_mobx:
      dependencies: ['mypackage']
      builders:
        # Disable moor builders. By default, those would run on each target
        moor_generator:
          enabled: false
        moor_generator|preparing_builder:
          enabled: false
        # we don't need to disable moor_generator_not_shared, because it's disabled by default

Any tips of what I did wrong?

Nevermind, I think it works now, I did nothing, it started to work right now, maybe is some kind of cache? (still, I executed code gen multiple times tho...).

Was this page helpful?
0 / 5 - 0 ratings