Flutterando / modular

A smart project structure
https://pub.dev/packages/flutter_modular
Other
1.31k stars 254 forks source link

UnregisteredInstance ERROR 🧨 (Flutter last version) #972

Open fabiototh opened 1 month ago

fabiototh commented 1 month ago

Describe the bug

Usei um FORK do modular, rodando em example. To simulate this: Mover i.addSingleton(SearchStore.new, config: storeConfig()); para um novo CoreModule:

class CoreModule extends Module {
  @override
  void binds(Injector i) {
    i.addSingleton(SearchStore.new, config: storeConfig());
    super.binds(i);
  }
}

Importar CoreModule no AppModule:

class AppModule extends Module {
  AppModule();

  @override
  void binds(Injector i) {
    i.addInstance<http.Client>(http.Client());
    i.add(GithubSearchDatasource.new);
    i.add(SearchRepositoryImpl.new);
    i.add(SearchByTextImpl.new);
  }

  @override
  void routes(Router r) {
    r.child('/', child: () => const SearchPage());
    r.child(
      '/details',
      child: () => DetailsPage(result: r.args.data),
      guards: [GuardT()],
    );
  }

  @override
  // TODO: implementar imports
  List<Module> get imports => [
        CoreModule(),
      ];
}

Ao executar, vocĂŞ recebe o seguinte erro:

The following UnregisteredInstance was thrown building _FocusInheritedScope: SearchByText not registered. SearchStore => SearchByText

Environment Add your flutter doctor -v

[✓] Flutter (Channel stable, 3.24.3, on macOS 15.0.1 24A348 darwin-arm64, locale pt-BR) • Flutter version 3.24.3 on channel stable at /Users/fabiocarlos/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 2663184aa7 (6 weeks ago), 2024-09-11 16:27:48 -0500 • Engine revision 36335019a8 • Dart version 3.5.3 • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/fabiocarlos/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.0) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 16A242d • CocoaPods version 1.15.2

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)

[✓] VS Code (version 1.94.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.98.0

[✓] Connected device (5 available) • 23129RA5FL (mobile) • d1a7c5b4 • android-arm64 • Android 14 (API 34) • iPhone de Fábio Carlos de Souza (mobile) • 00008030-0016481914C1802E • ios • iOS 18.0.1 22A3370 • macOS (desktop) • macos • darwin-arm64 • macOS 15.0.1 24A348 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 15.0.1 24A348 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 129.0.6668.101

[✓] Network resources • All expected network resources are available.

• No issues found!

Add your pubspec.yaml

```yaml
name: flutter_modular_example
description: A new Flutter project.

# 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.18.0 <3.0.0"

dependencies:
  dartz: ^0.10.0-nullsafety.2
  flutter_mobx: ^2.0.1
  flutter_triple: ^2.1.4
  http: ^1.1.0
  flutter_modular: 
    path: ../
  alice: ^0.4.2
  bloc: ^8.0.3
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.

dev_dependencies:
  flutter_lints: ^2.0.2
  flutter_test:
    sdk: flutter
  mocktail: ^0.3.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
vars:
  clean: flutter clean
  get: flutter pub get
  runner: flutter pub run build_runner
scripts:
  mobx_build: $clean & $get & $runner build --delete-conflicting-outputs
  mobx_watch: $clean & $get & $runner watch --delete-conflicting-outputs
# dependency_overrides:
#   flutter_modular:
#     path: ../../../modular/flutter_modular
# To add assets to your application, add an assets section, like this:
# assets:
#   - images/a_dot_burr.jpeg
#   - images/a_dot_ham.jpeg
# 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`
```

Add your Info.plist same of the modular example Add your AppDelegate.swift same of the modular example Add your MainActivity same of the modular example Add your AndroidManifest.xml same of the modular example Add your /app/res/values/strings.xml same of the modular example

To Reproduce Read Describe the bug

Expected behavior Modular working with CoreModule

fabiototh commented 1 month ago

@jacobaraujo7 please help me! God bless you.

eduardoflorence commented 1 month ago

@fabiototh, this correct is exportedBind (CoreModule):

class CoreModule extends Module {
  @override
  void exportedBinds(Injector i) {
    i.addSingleton(SearchStore.new, config: storeConfig());
  }
}
fabiototh commented 1 month ago

Hello, @eduardoflorence,

I was using Flutter version 3.16.0, then I updated the (large) project to the latest version. Before the update, everything was working perfectly.

This has happened to me before; it seems like flutter_modular “breaks” things.

I fixed it by putting everything into the same CoreModule, and by everything, I mean all the modules that were imported into the AppModule. Only then did it work. It’s really discouraging.

eu estava usando a versĂŁo do flutter 3.16.0 dai atualizei o projeto (grande) para a ultima. Antes estava tudo funcionando perfeitamente.

Isso já me aconteceu uma vez, parece que o flutter_modular "estraga".

Eu resolvi, colocando tudo no mesmo CoreModule, em tudo eu quero dizer: tudo que estava dentro de Module, que estava importado os mĂłdulos no AppModule. SĂł assim funcionou. Realmente Ă© algo desanimador.

eduardoflorence commented 1 month ago

Mas pode fazer como eu falei acima que funciona certinho. Eu uso o Flutter e Modular em suas últimas versões e funciona tudo perfeito.

fabiototh commented 1 month ago

O CoreModule estava exportBinds. E mesmo assim fala que UnregisteredInstance.

Recentemente criei um projeto na ultima versão do Flutter tamanho médio, e realmente não tive problemas nenhum.

Mas esse projeto especificamente, ao atualizar nĂŁo funciona de jeito nenhum.