作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 data_provider 包和一个存储库包。如果我在 data_provider 中运行 main.dart,一切正常,并且我得到了获取的数据。但是如果我想从存储库层获取数据,我会收到此错误:
lib/example/main.dart: Warning: Interpreting this as package URI, 'package:profile_repository/example/main.dart'.
lib/example/main.dart:1
Error: Couldn't resolve the package 'gql_code_builder' in 'package:gql_code_builder/src/serializers/operation_serializer.dart'.
Error: Couldn't resolve the package 'gql_code_builder' in 'package:gql_code_builder/src/serializers/default_scalar_serializer.dart'.
: Error: Not found: 'package:gql_code_builder/src/serializers/operation_serializer.dart'
../…/lib/serializers.gql.dart:135
import 'package:gql_code_builder/src/serializers/operation_serializer.dart'
^
: Error: Not found: 'package:gql_code_builder/src/serializers/default_scalar_serializer.dart'
../…/lib/schema.schema.gql.dart:7
import 'package:gql_code_builder/src/serializers/default_scalar_serializer.dart'
^
: Error: Method not found: 'OperationSerializer'.
../…/lib/serializers.gql.dart:141
..add(OperationSerializer())
^^^^^^^^^^^^^^^^^^^
: Error: Method not found: 'DefaultScalarSerializer'.
../…/lib/schema.schema.gql.dart:1380
static Serializer<GJSON> get serializer => _i2.DefaultScalarSerializer<GJSON>(
main.dart(存储库层)
// ignore_for_file: avoid_print, avoid_void_async, unnecessary_lambdas,
import 'package:data_provider/data_provider.dart';
import 'package:ferry/ferry.dart';
import 'package:get_it/get_it.dart';
import 'package:profile_repository/profile_repository.dart';
void main() async {
final client = await initClient();
GetIt.I.registerLazySingleton<Client>(() => client);
try {
final profiles = await ProfileRepository().getProfiles();
print('Profiles: $profiles');
} on Exception catch (e) {
print(e);
}
}
配置文件存储库:
import 'package:data_provider/data_provider.dart';
/// Thrown when an error occurs while looking up for profiles.
class ProfilesException implements Exception {}
/// {@template profile_repository}
/// Profiles from different categories fetched
/// {@endtemplate}
class ProfileRepository {
/// {@macro profile_repository}
ProfileRepository({ProfileService? profileService})
: _profileService = profileService ?? ProfileService();
final ProfileService _profileService;
/// Returns a list of all club and players profiles.
///
/// Throws a [ProfilesException] if an error occurs.
Future<List<GGetProfilesData_profiles>?> getProfiles() async {
try {
final profiles = await _profileService.getProfiles();
return profiles;
} on Exception {
throw ProfilesException();
}
}
}
profile_service(数据层):
// ignore_for_file: avoid_print
import 'package:built_collection/built_collection.dart';
import 'package:data_provider/data_provider.dart';
import 'package:ferry/ferry.dart';
import 'package:get_it/get_it.dart';
class ProfileService {
final client = GetIt.I<Client>();
Future<List<GGetProfilesData_profiles>?> getProfiles() async {
try {
final profilesReq = GGetProfilesReq();
BuiltList<GGetProfilesData_profiles>? result;
final value = await client.request(profilesReq).first;
RequestHelper.checkError(value);
if (value.data != null && value.data!.profiles != null) {
result = value.data!.profiles;
}
return result?.toList();
} on DataProviderGraphqlErrorException catch (e) {
print(e.toString());
} on DataProviderLinkException catch (e) {
print(e.toString());
}
return List.empty();
}
}
main.dart(data_provider,什么有效!)
// ignore_for_file: avoid_print, avoid_void_async, unnecessary_lambdas,
import 'package:data_provider/data_provider.dart';
import 'package:ferry/ferry.dart';
import 'package:get_it/get_it.dart';
void main() async {
final client = await initClient();
GetIt.I.registerLazySingleton<Client>(() => client);
try {
final profiles = await ProfileService().getProfiles();
print('Profiles: $profiles');
} on Exception catch (e) {
print(e);
}
}
最佳答案
我已经找到解决办法了。虽然不是完美的,但它确实有效。
@knaecke 引导我找到答案。您可以在主库上的 pubspec.yaml 的“dependency:”部分显式添加 gql_code_builder(而不是真正使用它的包)。
这很丑陋而且很奇怪,但是,它确实有效......
关于flutter - 错误: Couldn't resolve the package 'gql_code_builder' from repository layer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71080540/
我创建了一个 data_provider 包和一个存储库包。如果我在 data_provider 中运行 main.dart,一切正常,并且我得到了获取的数据。但是如果我想从存储库层获取数据,我会收到
我是一名优秀的程序员,十分优秀!