Files
cherrypick/cherrypick_annotations/lib/src/injectable.dart

45 lines
1.6 KiB
Dart
Raw Normal View History

2025-05-23 12:21:23 +03:00
//
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2025-08-08 23:24:05 +03:00
// https://www.apache.org/licenses/LICENSE-2.0
2025-05-23 12:21:23 +03:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
2025-05-23 15:26:09 +03:00
import 'package:meta/meta.dart';
/// Marks a class as injectable, enabling automatic field injection by CherryPick's code generator.
2025-05-23 17:13:57 +03:00
///
/// Use `@injectable()` on a class whose fields (marked with `@inject`) you want to be automatically injected from the DI [Scope].
/// When used together with code generation (see cherrypick_generator), a mixin will be generated to inject fields.
2025-05-23 17:13:57 +03:00
///
/// Example:
2025-05-23 17:13:57 +03:00
/// ```dart
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
///
2025-05-23 17:13:57 +03:00
/// @injectable()
/// class ProfileScreen with _\$ProfileScreen {
/// @inject()
/// late final UserManager manager;
///
2025-05-23 17:13:57 +03:00
/// @inject()
/// @named('main')
/// late final ApiClient api;
2025-05-23 17:13:57 +03:00
/// }
///
/// // After running build_runner, call:
/// // profileScreen.injectFields();
2025-05-23 17:13:57 +03:00
/// ```
///
/// After running the generator, the mixin (`_\$ProfileScreen`) will be available to help auto-inject all [@inject] fields in your widget/service/controller.
2025-05-23 15:26:09 +03:00
@experimental
2025-05-23 12:21:23 +03:00
final class injectable {
/// Creates an [injectable] annotation for classes.
2025-05-23 12:21:23 +03:00
const injectable();
}