PdfViewPinch displays blank screen and throws Null check operator used on a null value error
MichelMelhem opened this issue · 1 comments
MichelMelhem commented
When trying to use PdfViewPinch within a custom PdfSandBox widget, the screen displays blank, and the following error is thrown:
E/flutter ( 9704): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value
E/flutter ( 9704): #0 _PdfViewPinchState._updatePageState (package:pdfx/src/viewer/pinch/pdf_view_pinch.dart:363:44)
E/flutter ( 9704): <asynchronous suspension>
However, when embedding PdfViewPinch directly in the parent widget, it works as expected and the PDF is displayed correctly.
Steps to Reproduce:
- Define a PdfSandBox widget that uses PdfViewPinch.
- Integrate the PdfSandBox widget in a parent widget using a switch statement to control the displayed tab.
- Run the app and switch to the tab containing the PdfSandBox.
Expected Behavior:
The PdfViewPinch should display the PDF document correctly without any errors.
Actual Behavior:
The screen remains blank, and the error mentioned above is thrown.
Code Samples:
PdfSandBox Widget:
dart
import 'dart:async';
import 'package:best_report/src/models/Observation.model.dart';
import 'package:best_report/src/models/PdfSandboxArgs.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:pdfx/pdfx.dart';
typedef void OnSandboxLoaded(PdfSandBoxController controller);
class PdfSandBox extends StatefulWidget {
final PdfSandboxArgs args;
final PdfControllerPinch pdfControllerPinch;
final OnSandboxLoaded _onSandboxLoaded;
const PdfSandBox(this.args, this._onSandboxLoaded, this.pdfControllerPinch,
{Key? key})
: super(key: key);
@override
State<PdfSandBox> createState() => _PdfSandBoxState();
}
class _PdfSandBoxState extends State<PdfSandBox> {
final Completer<PdfSandBoxController> _controller =
Completer<PdfSandBoxController>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("PDF Viewer")),
body: Center(
child: Container(
width: double.infinity,
height: double.infinity,
child: PdfViewPinch(
controller: widget.pdfControllerPinch,
onDocumentLoaded: (document) {
debugPrint('PDF document loaded');
final PdfSandBoxController controller =
PdfSandBoxController(widget);
_controller.complete(controller);
widget._onSandboxLoaded(controller);
},
onError: (error) {
debugPrint('Error loading PDF: $error');
},
),
),
),
);
}
}
class PdfSandBoxController {
PdfSandBox _widget;
PdfSandBoxController(this._widget);
Future<void> setObservations(List<Observation> observations) async {
try {
// Your implementation
} catch (error) {
debugPrint('Error in setObservations: $error');
}
}
Future<void> editObservation(Observation observation) async {
try {
// Your implementation
} catch (error) {
debugPrint('Error in editObservation: $error');
}
}
Future<void> setDarkMode(Brightness brightness) async {
try {
if (defaultTargetPlatform == TargetPlatform.android) {
// Your implementation
}
} catch (error) {
debugPrint('Error in setDarkMode: $error');
}
}
Future<void> addObservation(Observation observation) async {
try {
// Your implementation
} catch (error) {
debugPrint('Error in addObservation: $error');
}
}
Future<void> updateObservations(List<Observation> observations) async {
try {
// Your implementation
} catch (error) {
debugPrint('Error in updateObservations: $error');
}
}
}
Parent Widget:
dart
switch (tabItem) {
case TabItem.drawing:
String? pdfPath = widget.args.plan?.path;
if (pdfPath == null || !File(pdfPath).existsSync()) {
debugPrint('PDF file does not exist at path: $pdfPath');
return Center(child: Text("PDF file not found"));
}
return PdfSandBox(
widget.args,
(controller) {
this._controller = controller;
debugPrint('PdfSandBoxController initialized');
},
PdfControllerPinch(
document: PdfDocument.openFile(pdfPath),
),
);
case TabItem.buildingTrade:
return Container(
child: MediaQuery.of(context).orientation == Orientation.portrait
? Column(
children: observationList,
)
: Row(
children: observationList,
),
);
default:
return Center(
child: Text("404 page not found"),
);
}
Additional Context:
- PdfX version : 2.6.0
- OS: Android
- Flutter doctor :
- Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.22.2, on macOS 14.5 23F79 darwin-arm64, locale en-FR)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[!] Xcode - develop for iOS and macOS (Xcode 15.4)
✗ Unable to get list of installed Simulator runtimes.
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.90.1)
[✓] Connected device (4 available)
[✓] Network resources
Any help in resolving this issue would be greatly appreciated. Thank you!
neverCase commented
Is there any solution to fix this problem?
openFile or openData doesn't work