在 Flutter 中构建 iOS 应用程序时键入“ui.ViewConfiguration”未找到错误

Muh*_*nas 7 ios dart flutter

错误:找不到类型“ui.ViewConfiguration”。

\n

在运行我的 flutter 项目时,我收到此错误并且构建失败。它在 Android 上运行良好,但仅在 ios 中运行时才发现问题。

\n
\n

无法构建 iOS 应用程序错误(Xcode):\n../../../../.pub-cache/hosted/pub.dev/printing-5.9.3/lib/src/widget_wrapper.dart:234 :9:\n错误:找不到类型\'ui.ViewConfiguration\'。\n../\xe2\x80\xa6/src/widget_wrapper.dart:234

\n
\n

无法为模拟器构建应用程序。\n在 iPhone 14 上启动应用程序时出错。\n已退出

\n

这是发生错误的代码。这是一个 .pubcache 文件。

\n
import \'dart:async\';\nimport \'dart:typed_data\';\nimport \'dart:ui\' as ui;\n\nimport \'package:flutter/material.dart\';\nimport \'package:flutter/rendering.dart\';\nimport \'package:flutter/widgets.dart\';\nimport \'package:pdf/pdf.dart\';\nimport \'package:pdf/widgets.dart\' as pw;\n\n/// ImageProvider that draws a Flutter Widget on a PDF document\nclass WidgetWraper extends pw.ImageProvider {\n  WidgetWraper._(\n    this.bytes,\n    int width,\n    int height,\n    PdfImageOrientation orientation,\n    double? dpi,\n  ) : super(width, height, orientation, dpi);\n\n  /// Wrap a Flutter Widget identified by a GlobalKey to an ImageProvider.\n  ///\n  /// Use it with a RepaintBoundary:\n  /// \n  /// final rb = GlobalKey();\n  ///\n  /// @override\n  /// Widget build(BuildContext context) {\n  ///   return RepaintBoundary(\n  ///       key: rb,\n  ///       child: FlutterLogo()\n  ///   );\n  /// }\n  ///\n  /// Future<Uint8List> _generatePdf(PdfPageFormat format) async {\n  ///   final pdf = pw.Document();\n  ///\n  ///   final image = await WidgetWraper.fromKey(key: rb);\n  ///\n  ///   pdf.addPage(\n  ///     pw.Page(\n  ///       build: (context) {\n  ///         return pw.Center(\n  ///           child: pw.Image(image),\n  ///         );\n  ///       },\n  ///     ),\n  ///   );\n  ///\n  ///   return pdf.save();\n  /// }\n  /// \n  static Future<WidgetWraper> fromKey({\n    required GlobalKey key,\n    int? width,\n    int? height,\n    double pixelRatio = 1.0,\n    PdfImageOrientation? orientation,\n    double? dpi,\n  }) async {\n    assert(pixelRatio > 0);\n\n    final wrappedWidget =\n        key.currentContext!.findRenderObject() as RenderRepaintBoundary;\n    final image = await wrappedWidget.toImage(pixelRatio: pixelRatio);\n    final byteData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);\n\n    if (byteData == null) {\n      return WidgetWraper._(\n        Uint8List(0),\n        0,\n        0,\n        PdfImageOrientation.topLeft,\n        dpi,\n      );\n    }\n\n    final imageData = byteData.buffer.asUint8List();\n    return WidgetWraper._(\n      imageData,\n      image.width,\n      image.height,\n      orientation ?? PdfImageOrientation.topLeft,\n      dpi,\n    );\n  }\n\n  /// Wrap a Flutter Widget to an ImageProvider.\n  ///\n  /// \n  /// final wrapped = await WidgetWraper.fromWidget(\n  ///   widget: Container(\n  ///     color: Colors.white,\n  ///     child: Text(\n  ///       \'Hello world !\',\n  ///       style: TextStyle(color: Colors.amber),\n  ///     ),\n  ///   ),\n  ///   constraints: BoxConstraints(maxWidth: 100, maxHeight: 400),\n  ///   pixelRatio: 3,\n  /// );\n  ///\n  /// pdf.addPage(\n  ///   pw.Page(\n  ///     pageFormat: format,\n  ///     build: (context) {\n  ///       return pw.Image(wrapped, width: 100);\n  ///     },\n  ///   ),\n  /// );\n  /// \n  static Future<WidgetWraper> fromWidget({\n    required Widget widget,\n    required BoxConstraints constraints,\n    double pixelRatio = 1.0,\n    PdfImageOrientation? orientation,\n    double? dpi,\n  }) async {\n    assert(pixelRatio > 0);\n\n    if (!constraints.hasBoundedHeight || !constraints.hasBoundedHeight) {\n      throw Exception(\n          \'Unable to convert an unbounded widget. Add maxWidth and maxHeight to the constraints.\');\n    }\n\n    widget = ConstrainedBox(\n      constraints: constraints,\n      child: widget,\n    );\n\n    final _properties = DiagnosticPropertiesBuilder();\n    widget.debugFillProperties(_properties);\n\n    if (_properties.properties.isEmpty) {\n      throw ErrorDescription(\'Unable to get the widget properties\');\n    }\n\n    final _constraints = _properties.properties\n        .whereType<DiagnosticsProperty<BoxConstraints>>()\n        .first\n        .value;\n\n    if (_constraints == null ||\n        !_constraints.hasBoundedWidth ||\n        !_constraints.hasBoundedWidth) {\n      throw Exception(\'Unable to convert an unbounded widget.\');\n    }\n\n    final _repaintBoundary = RenderRepaintBoundary();\n\n    final renderView = RenderView(\n      child: RenderPositionedBox(\n          alignment: Alignment.center, child: _repaintBoundary),\n      configuration: ViewConfiguration(\n          size: Size(_constraints.maxWidth, _constraints.maxHeight),\n          devicePixelRatio: ui.window.devicePixelRatio),\n      window: _FlutterView(\n        configuration: ui.ViewConfiguration(\n          devicePixelRatio: ui.window.devicePixelRatio,\n        ),\n      ),\n    );\n\n    final pipelineOwner = PipelineOwner()..rootNode = renderView;\n    renderView.prepareInitialFrame();\n\n    final buildOwner = BuildOwner(focusManager: FocusManager());\n    final _rootElement = RenderObjectToWidgetAdapter<RenderBox>(\n      container: _repaintBoundary,\n      child: Directionality(\n        textDirection: TextDirection.ltr,\n        child: IntrinsicHeight(child: IntrinsicWidth(child: widget)),\n      ),\n    ).attachToRenderTree(buildOwner);\n\n    buildOwner\n      ..buildScope(_rootElement)\n      ..finalizeTree();\n\n    pipelineOwner\n      ..flushLayout()\n      ..flushCompositingBits()\n      ..flushPaint();\n\n    final image = await _repaintBoundary.toImage(pixelRatio: pixelRatio);\n    final bytes = await image.toByteData(format: ui.ImageByteFormat.rawRgba);\n    if (bytes == null) {\n      throw Exception(\'Unable to read image data\');\n    }\n\n    return WidgetWraper._(\n      bytes.buffer.asUint8List(),\n      image.width,\n      image.height,\n      orientation ?? PdfImageOrientation.topLeft,\n      dpi,\n    );\n  }\n\n  /// The image data\n  final Uint8List bytes;\n\n  @override\n  PdfImage buildImage(pw.Context context, {int? width, int? height}) {\n    return PdfImage(\n      context.document,\n      image: bytes,\n      width: width ?? this.width!,\n      height: height ?? this.height!,\n      orientation: orientation,\n    );\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
class _FlutterView extends ui.FlutterView {\n  _FlutterView({required this.configuration});\n\n  final ui.ViewConfiguration configuration;\n\n  @override\n  ui.PlatformDispatcher get platformDispatcher =>\n      ui.PlatformDispatcher.instance;\n\n  @override\n  ui.ViewConfiguration get viewConfiguration => configuration;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这个特定部分给出了错误

\n
final ui.ViewConfiguration configuration;\n\n  @override\n  ui.PlatformDispatcher get platformDispatcher =>\n      ui.PlatformDispatcher.instance;\n\n  @override\n  ui.ViewConfiguration get viewConfiguration => configuration;\n\n
Run Code Online (Sandbox Code Playgroud)\n

cha*_*ler 7

printing我们还通过将软件包更新为https://pub.dev/packages/printing5.10.4上的最新版本来解决相同的问题

由于 Flutter 的最新更新,我们不得不更新其他一些包,但是一旦解决了依赖问题,这个错误就消失了。