flutter - Dart 分析检测到未使用的字段......我错过了什么?
问题描述
从我的代码示例中可以看出,我正在使用这个变量。我也在课后多次引用。
颤振警告 - 信息:未使用字段“_loadTimer”的值。([app] lib/models/knowledge_level/pb_cycle_permissions_collection.dart:12 处的未使用字段)
ng 是: info:未使用字段“_loadTimer”的值。([app] lib/models/knowledge_level/pb_cycle_permissions_collection.dart:12 处的未使用字段)
import 'dart:async';
import 'dart:collection';
import 'package:app/data/graphql/queries.dart';
import 'package:app/helpers/shared_logger.dart';
import 'package:flutter/cupertino.dart';
import '../command_permission.dart';
class PBCyclePermissionsCollection
with ListMixin<CommandPermission>, ChangeNotifier {
Timer? _loadTimer;
///
/// CONSTRUCTION AND INITIALIZATION
///
static final PBCyclePermissionsCollection _instance =
PBCyclePermissionsCollection._internal();
factory PBCyclePermissionsCollection() {
return _instance;
}
/// ACCESS SINGLETON VIA myPBCyclePermInstance = PBCyclePermissionsCollection()
PBCyclePermissionsCollection._internal() {
_loadTimer = Timer(_waitFirstLoad, _attemptLoad);
}
///
/// PRIVATE VARIABLES AND METHODS
///
static final Duration _waitFirstLoad = Duration(milliseconds: 500);
static final Duration _waitRetryLoad = Duration(seconds: 2);
static final int _maxAttempts = 4;
int _loadAttempts = 0;
bool _isReady = false;
bool _hasFailed = false;
/// Storage of CommandPermissions List once loaded
final List<CommandPermission> _list = [];
void _attemptLoad() async {
_loadAttempts++;
SharedLogger.I().d('_attemptLoad() current load attempt: ${_loadAttempts}');
try {
final results = await Queries.getCommandPermissions();
var data = results.data!['commandPermissions'];
var permissions = <CommandPermission>[];
for (var item in data) {
permissions.add(CommandPermission.fromJson(item));
}
/// Populated class with loaded objects.
_list.clear();
_list.addAll(permissions);
_isReady = true;
notifyListeners();
} catch (e) {
SharedLogger.I().e('Error loading PBCycle Permissions - ${e}');
_newAttempt();
}
}
void _newAttempt() {
SharedLogger.I().d(
'_newTry() _loadAttempts: ${_loadAttempts} _maxAttempts:${_maxAttempts} '
'creating new loadTimer for another try? : ${!(_loadAttempts >= _maxAttempts)}');
if (_loadAttempts >= _maxAttempts) {
_hasFailed = true;
notifyListeners();
// TODO: do we invalidate any existing data that may have been loaded before? Like if this load cycle is a refresh?
// If so, we should reset _isReady and _list;
return;
}
_loadTimer = Timer(_waitRetryLoad, _attemptLoad);
}
///
/// PUBLIC METHODS
///
bool get isLoaded {
return _isReady;
}
bool get hasFailed {
return _hasFailed;
}
@override
set length(int newLength) {
throw ('length cannot be changed externally');
}
@override
int get length {
return _list.length;
}
@override
CommandPermission operator [](int index) {
return _list[index];
}
@override
void operator []=(int index, CommandPermission value) {
throw ('Cannot modify list from outside');
}
}
解决方案
推荐阅读
- perl - 如何将 if/else 更改为哈希函数?perl
- c - 链表 - 获取分段错误
- html - 如何将父级的父级宽度应用于相对定位的子级 div?
- html - 带有自动播放和覆盖文本的纯 CSS 3 图像滑块
- python - Windows 10 操作系统上的标准 Python 模块在哪里?
- django - 导出 Django 表2
- reactjs - 如何在 React Js 中仅上传 PDF 文件和预览
- bash - 如何使用 shell 脚本将文件从 Amazon s3 移动到 sftp
- emacs - emacs/evil-mode:如何使 Ci 在 c-mode 激活时作为“evil-jump-forward”工作,并在 org-mode avtivated 时作为“org-cycle”工作
- ruby-on-rails - Heroku 未检测到 Ruby 版本