首页 > 解决方案 > 在颤动中创建了不必要的对象并占用了内存

问题描述

我有一个颤振应用程序,我在其中安装了一个 qr_flutter 包来呈现二维码。我的应用程序在加载时登陆登录屏幕,并且我没有使用任何与二维码相关的东西。当我打开 DevTools 并在内存视图中拍摄快照时,我可以看到一些对象已创建并占用与包 qr_flutter 相关的内存。

FinderPatternPosition
QrCodeElement
QrValidationStatus

qr_flutter 包中有枚举。有关他包的更多详细信息https://pub.dev/documentation/qr_flutter/latest/qr_flutter/qr_flutter-library.html

应用程序打开并显示登录页面后的内存快照

问题是为什么要创建这些对象?这就是包在颤振中安装后的工作方式吗?

标签: flutter

解决方案


是的。这些Enums定义在types.dart. 你可以参考源代码

paint_cache.dart使用那些枚举https://github.com/lukef/qr.flutter/blob/master/lib/src/paint_cache.dart

QrValidationStatushttps://github.com/lukef/qr.flutter/blob/master/lib/src/validator.dart中定义

/// The status of the QR code data you requested to be validated.
enum QrValidationStatus {
  /// The QR code data is valid for the provided parameters.
  valid,

  /// The QR code data is too long for the provided version + error check
  /// configuration or too long to be contained in a QR code.
  contentTooLong,

  /// An unknown / unexpected error occurred when we tried to validate the QR
  /// code data.
  error,
}

FinderPatternPositionQrCodeElementhttps://github.com/lukef/qr.flutter/blob/master/lib/src/types.dart中定义

/*
 * QR.Flutter
 * Copyright (c) 2019 the QR.Flutter authors.
 * See LICENSE for distribution and usage details.
 */

import 'dart:ui';

import 'package:flutter/widgets.dart';

/// Represents a specific element / part of a QR code. This is used to isolate
/// the different parts so that we can style and modify specific parts
/// independently.
enum QrCodeElement {
  /// The 'stroke' / outer square of the QR code finder pattern element.
  finderPatternOuter,

  /// The inner/in-between square of the QR code finder pattern element.
  finderPatternInner,

  /// The "dot" square of the QR code finder pattern element.
  finderPatternDot,

  /// The individual pixels of the QR code
  codePixel,

  /// The "empty" pixels of the QR code
  codePixelEmpty,
}

/// Enumeration representing the three finder pattern (square 'eye') locations.
enum FinderPatternPosition {
  /// The top left position.
  topLeft,

  /// The top right position.
  topRight,

  /// The bottom left position.
  bottomLeft,
}

/// Styling options for any embedded image overlay
class QrEmbeddedImageStyle {
  /// Create a new set of styling options.
  QrEmbeddedImageStyle({
    this.size,
    this.color,
  });

  /// The size of the image. If one dimension is zero then the other dimension
  /// will be used to scale the zero dimension based on the original image
  /// size.
  Size size;

  /// Color to tint the image.
  Color color;

  /// Check to see if the style object has a non-null, non-zero size.
  bool get hasDefinedSize => size != null && size.longestSide > 0;

  @override
  int get hashCode => size.hashCode ^ color.hashCode;

  @override
  bool operator ==(Object other) {
    if (other is QrEmbeddedImageStyle) {
      return size == other.size && color == other.color;
    }
    return false;
  }
}

推荐阅读