Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding abstract classes to implementations not working #472

Open
devnta opened this issue Jun 17, 2024 · 0 comments
Open

Binding abstract classes to implementations not working #472

devnta opened this issue Jun 17, 2024 · 0 comments

Comments

@devnta
Copy link

devnta commented Jun 17, 2024

First of all, sorry for my bad English.
I get an error:

Bad state: GetIt: Object/factory with type MovieUseCaseImpl is not registered inside GetIt. 
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?)
[✓] Flutter (Channel stable, 3.22.2, on macOS 14.0 23A344 darwin-arm64, locale en-VN)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Android Studio (version 2023.3)
[✓] VS Code (version 1.89.1)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

injectable_generator: ^2.6.0 or injectable_generator: ^2.6.1 all has error injection.dart

import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';

import 'injection.config.dart';

final locator = GetIt.instance;
@injectableInit
void configureDependencies() {
  locator.init();
  log('>>>>>>>>>> Test: ${locator.isRegistered<MovieUseCaseImpl>()} --- ${locator.isRegistered<MovieUseCase>()}');
  
}

Result: [log] >>>>>>>>>> Test: false --- true

main.dart

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  configureDependencies();
  runApp(const MyApp());
}

MovieUseCaseImpl

@Injectable(as: MovieUseCase)
class MovieUseCaseImpl extends MovieUseCase {
  final MovieRepository repository;

  MovieUseCaseImpl(this.repository);

  @override
  Future<Either<Exception, List<Movie>>> fetchMovie() async {
    final response = await repository.fetchMovies();
    return response.fold((left) {
      return Left(left);
    }, (right) {
      final movies = <Movie>[];
      right.map((e) => movies.add(Movie.fromResponse(e))).toList();
      return Right(movies);
    });
  }
}

MovieRepositoryImpl

@Injectable(as: MovieRepository)
class MovieRepositoryImpl implements MovieRepository {
  final MovieDataSource dataSource;

  MovieRepositoryImpl(this.dataSource);

  @override
  Future<Either<Exception, List<MovieResponse>>> fetchMovies() {
    return dataSource.fetchMovies();
  }
}

MovieRemoteDataSourceImpl

@Injectable(as: MovieDataSource)
class MovieRemoteDataSourceImpl implements MovieDataSource {
  final Dio dio;

  MovieRemoteDataSourceImpl(this.dio);

  @override
  Future<Either<Exception, List<MovieResponse>>> fetchMovies() async {
    const url = APIConstant.topRatedMovie;
    try {
      dio.interceptors.add(AuthenticationInterceptor());
      final response = await dio.get(url);
      final mapFromJson = response.data['results'] as List;
      final result = mapFromJson.map((e) {
        return MovieResponse.fromJson(e);
      }).toList();
      return Right(result);
    } on Exception catch (e) {
      return Left(e);
    }
  }
}

image
I have following this instruction.
Did I miss something?
@Milad-Akarie Sorry for mention

yejun614 added a commit to yejun614/flutter_server_driven_ui that referenced this issue Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant