AvoidDirectDio class
禁止业务代码直接 new Dio 类型或 import package:dio/dio.dart。
触发场景(任一):
Dio(...)、BaseOptions(...)、Options(...)、Interceptor()子类直接new;RequestOptions(...)/Response(...)/FormData(...)/MultipartFile(...)等 Dio 类型的实例化。
不禁止:调用 flutter_spine 暴露的 HttpClient / DioHttpConfig /
Interceptor 等已封装类型。
为什么
- 业务代码绕过
HttpClient抽象,走裸 Dio → 错误归一缺失(拿不到 AppException); - 测试时无法用
MockHttpClient/_FakeAdapter注入; DioHttpConfig.interceptors等"声明式"开关失效;MultipartFile、FormData等类型直接出现在业务里,会让业务依赖 dio 版本号。
改写指引
// ❌ 业务代码
final dio = Dio(BaseOptions(baseUrl: 'https://api'));
final res = await dio.get('/users/me');
// ✅ 通过注入 HttpClient
final client = ref.read(httpClientProvider);
final user = await client.get<User>(
'/users/me',
decoder: (j) => User.fromJson(j! as Map<String, dynamic>),
);
// ❌ 业务代码组装 multipart
final form = FormData.fromMap({'avatar': MultipartFile.fromFileSync(path)});
await dio.post('/upload', data: form);
// ✅ 用 MultipartFilePart + HttpClient.upload
await client.upload<UploadResult>(
'/upload',
files: [MultipartFilePart.fromPath(field: 'avatar', filePath: path)],
decoder: (j) => UploadResult.fromJson(j! as Map<String, dynamic>),
);
豁免
flutter_spine自身的 HTTP 实现:package/flutter_spine/lib/src/network/**- 应用启动
/lib/main.dart/lib/bootstrap.dart - 测试 / fixture / example 路径
Constructors
- AvoidDirectDio()
-
const
Properties
- code → LintCode
-
The
LintCodethat thisLintRulemay emit.finalinherited - enabledByDefault → bool
-
Whether the lint rule is on or off by default in an empty analysis_options.yaml
no setterinherited
-
filesToAnalyze
→ List<
String> -
A list of glob patterns matching the files that run cares about.
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
getFixes(
) → List< Fix> -
Obtains the list of
Fixassociated with thisLintRule.inherited -
isEnabled(
CustomLintConfigs configs) → bool -
Checks whether this lint rule is enabled in a configuration file.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
run(
CustomLintResolver resolver, ErrorReporter reporter, CustomLintContext context) → void - Emits lints for a given file.
-
startUp(
CustomLintResolver resolver, CustomLintContext context) → Future< void> -
Emits lints for a given file.
inherited
-
testAnalyzeAndRun(
File file) → Future< List< AnalysisError> > -
Analyze a Dart file and runs this assist in test mode.
inherited
-
testRun(
ResolvedUnitResult result, {Pubspec? pubspec}) → Future< List< AnalysisError> > -
Runs this assist in test mode.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited