首页 > 解决方案 > 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');
  }
}

带有代码示例和相关 Dart 分析提示的 IDE 图像

标签: flutterdart

解决方案


推荐阅读