正如你从我的代码示例中看到的,我使用了这个变量,并且在后面的课程中多次引用它。
Flutter警告-信息:未使用字段“_loadTimer”的值。(未使用的字段位于[app]库/模型/知识级别/pb_cycle_permissions_collection.dart:12)
ng为:信息:未使用字段“_loadTimer”的值。(未使用的字段位于[app]库/模型/知识级别/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');
}
}
Image of IDE with Code Sample and associated Dart Analysis Hints
2条答案
按热度按时间fkaflof61#
您实际上并没有使用它,只是多次设置该值
u7up0aaq2#
Andrew的回答是正确的,但由于不确定“it”指的是什么,所以有点不清楚。下面是解释警告消息含义的另一种方法:
请注意,该消息指出您没有使用 value。您正在使用变量,但没有使用其值。您正在分配值。读取值将是使用它。
也就是说,问题已经得到了解答,但我认为问“我错过了什么”这个问题有些模糊,你(OP)想要达到什么目的?我想是再也看不到那个警告了。这就是我写这篇文章的原因。我也有类似的问题。我也有一个计时器的类变量,我得到了这个相同的警告消息。一个人不需要为了 * 使用 * 而读取值一个计时器,但是分析器不知道。在写这个响应的时候,我发现你可以隐藏一个警告。这个怎么样: