Skip to content

Commit eeb8ac9

Browse files
Replace some nullable types with late types
1 parent 4addab6 commit eeb8ac9

File tree

2 files changed

+35
-43
lines changed

2 files changed

+35
-43
lines changed

example/test/pdf_text_test.dart

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@Skip(
2-
'To run these tests, make sure an emulator or a real device is connected, then \'cd example\' and : \'flutter run test/pfd_text_test.dart\'')
2+
'To run these tests, make sure an emulator or a real device is connected, then \'cd example\' and : \'flutter run test/pdf_text_test.dart\'')
33
import 'dart:io';
44

55
import 'package:flutter/foundation.dart';
@@ -21,13 +21,13 @@ void main() async {
2121

2222
final testDirectoryPath =
2323
join((await getTemporaryDirectory()).path, "temp_pdf_text_dir");
24-
final PdfTestUtils pdfUtils = PdfTestUtils(testDirectoryPath);
25-
final File encryptedPdf = File(join(testDirectoryPath, "encrypted.pdf"));
24+
final PdfTestUtils pdfUtils = PdfTestUtils(testDirectoryPath);
25+
final File encryptedPdf = File(join(testDirectoryPath, "encrypted.pdf"));
2626

2727
setUpAll(() async {
2828
/// init mock http client so that the PDFDoc.fromUrl() obtains the mocked version
2929
ClientProvider(
30-
client: MockClient((Request req) => Future.value(Response.bytes(
30+
client: MockClient((Request req) => Future.value(Response.bytes(
3131
File(join(testDirectoryPath, req.url.path.split("/").last))
3232
.readAsBytesSync(),
3333
200))));
@@ -56,7 +56,7 @@ void main() async {
5656
subject: "The most profound theological book ever written",
5757
keywords: "Doctor,Angelicus");
5858

59-
List<String > page = [
59+
List<String> page = [
6060
"Et ut intentio nostra sub aliquibus certis limitibus comprehendatur,",
6161
"necessarium est primo investigare de ipsa sacra doctrina, qualis sit, et ad quæ se extendat.",
6262
"Circa quæ quærenda sunt decem.",
@@ -65,11 +65,11 @@ void main() async {
6565
"Tertio, utrum sit una vel plures."
6666
];
6767

68-
File pdf = await pdfUtils.createPdfFile([page], info: info);
68+
File pdf = await pdfUtils.createPdfFile([page], info: info);
6969

70-
PDFDoc fromFile = await PDFDoc.fromFile(pdf);
71-
PDFDoc fromPath = await PDFDoc.fromPath(pdf.path);
72-
PDFDoc fromUrl =
70+
PDFDoc fromFile = await PDFDoc.fromFile(pdf);
71+
PDFDoc fromPath = await PDFDoc.fromPath(pdf.path);
72+
PDFDoc fromUrl =
7373
await PDFDoc.fromURL("http://localhost/${basename(pdf.path)}");
7474

7575
await forEach([fromFile, fromPath, fromUrl], (dynamic doc) async {
@@ -91,24 +91,24 @@ void main() async {
9191
subject: "Detecitve stories",
9292
keywords: "Holmes,Watson");
9393

94-
List<String > page1 = [
94+
List<String> page1 = [
9595
"To Sherlock Holmes she is always the woman.",
9696
"I have seldom heard him mention her under any other name.",
9797
"In his eyes she eclipses and predominates the whole of her sex.",
9898
];
9999

100-
List<String > page2 = [
100+
List<String> page2 = [
101101
"himself in a false position. He never spoke of the softer",
102102
"They were admirable things for the observer-excellent for",
103103
"But for the trained reasoner to admit such intrusions into",
104104
"was to introduce a distracting factor which might throw a",
105105
];
106106

107-
File pdf = await pdfUtils.createPdfFile([page1, page2], info: info);
107+
File pdf = await pdfUtils.createPdfFile([page1, page2], info: info);
108108

109-
PDFDoc fromFile = await PDFDoc.fromFile(pdf);
110-
PDFDoc fromPath = await PDFDoc.fromPath(pdf.path);
111-
PDFDoc fromUrl =
109+
PDFDoc fromFile = await PDFDoc.fromFile(pdf);
110+
PDFDoc fromPath = await PDFDoc.fromPath(pdf.path);
111+
PDFDoc fromUrl =
112112
await PDFDoc.fromURL("http://localhost/${basename(pdf.path)}");
113113

114114
await forEach([fromFile, fromPath, fromUrl], (dynamic doc) async {
@@ -126,11 +126,11 @@ void main() async {
126126
test("should read text from a password protected pdf file", () async {
127127
expect(encryptedPdf.existsSync(), true);
128128

129-
PDFDoc fromFile =
129+
PDFDoc fromFile =
130130
await PDFDoc.fromFile(encryptedPdf, password: "password");
131-
PDFDoc fromPath =
131+
PDFDoc fromPath =
132132
await PDFDoc.fromPath(encryptedPdf.path, password: "password");
133-
PDFDoc fromUrl = await PDFDoc.fromURL(
133+
PDFDoc fromUrl = await PDFDoc.fromURL(
134134
"http://localhost/${basename(encryptedPdf.path)}",
135135
password: "password");
136136

@@ -145,7 +145,7 @@ void main() async {
145145
test("should fail if the password is invalid", () async {
146146
expect(encryptedPdf.existsSync(), true);
147147

148-
int exceptionCount = 0;
148+
int exceptionCount = 0;
149149

150150
final fromFile =
151151
() => PDFDoc.fromFile(encryptedPdf, password: "invalid-password");
@@ -158,7 +158,7 @@ void main() async {
158158
await forEach([fromFile, fromPath, fromURL], (dynamic foo) async {
159159
try {
160160
await foo();
161-
} on PlatformException catch (e) {
161+
} on PlatformException catch (e) {
162162
expect(
163163
e.message,
164164
contains(isIos
@@ -174,4 +174,4 @@ void main() async {
174174
reason: "All attempts with invalid password must fail!");
175175
});
176176
});
177-
}
177+
}

lib/pdf_text.dart

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const String _TEMP_DIR_NAME = ".flutter_pdf_text";
1414
/// to be used: [PDFDoc.fromFile], [PDFDoc.fromPath], [PDFDoc.fromURL].
1515
class PDFDoc {
1616
late File _file;
17-
PDFDocInfo? _info;
18-
List<PDFPage>? _pages;
19-
String? _password;
17+
late PDFDocInfo _info;
18+
late List<PDFPage> _pages;
19+
late String _password;
2020

2121
PDFDoc._internal();
2222

@@ -26,18 +26,14 @@ class PDFDoc {
2626
var doc = PDFDoc._internal();
2727
doc._password = password;
2828
doc._file = file;
29-
Map? data;
29+
late Map data;
3030
try {
3131
data = await _CHANNEL
3232
.invokeMethod('initDoc', {"path": file.path, "password": password});
3333
} on Exception catch (e) {
3434
return Future.error(e);
3535
}
36-
doc._pages =
37-
List.generate(data!["length"], (index) => PDFPage._fromDoc(doc, index));
38-
// for (int i = 0; i < data!["length"]; i++) {
39-
// doc._pages!.add(PDFPage._fromDoc(doc, i));
40-
// }
36+
doc._pages = List.generate(data["length"], (i) => PDFPage._fromDoc(doc, i));
4137
doc._info = PDFDocInfo._fromMap(data["info"]);
4238
return doc;
4339
}
@@ -71,27 +67,27 @@ class PDFDoc {
7167
}
7268

7369
/// Gets the page of the document at the given page number.
74-
PDFPage pageAt(int pageNumber) => _pages![pageNumber - 1];
70+
PDFPage pageAt(int pageNumber) => _pages[pageNumber - 1];
7571

7672
/// Gets the pages of this document.
7773
/// The pages indexes start at 0, but the first page has number 1.
7874
/// Therefore, if you need to access the 5th page, you will do:
7975
/// var page = doc.pages[4]
8076
/// print(page.number) -> 5
81-
List<PDFPage>? get pages => _pages;
77+
List<PDFPage> get pages => _pages;
8278

8379
/// Gets the number of pages of this document.
84-
int get length => _pages!.length;
80+
int get length => _pages.length;
8581

8682
/// Gets the information of this document.
87-
PDFDocInfo? get info => _info;
83+
PDFDocInfo get info => _info;
8884

8985
/// Gets the entire text content of the document.
9086
Future<String> get text async {
9187
// Collecting missing pages
9288

9389
List<int> missingPagesNumbers = [];
94-
_pages!.forEach((page) {
90+
_pages.forEach((page) {
9591
if (page._text == null) {
9692
missingPagesNumbers.add(page.number);
9793
}
@@ -117,12 +113,8 @@ class PDFDoc {
117113
pageAt(missingPagesNumbers[i])._text = missingPagesTexts[i];
118114
}
119115

120-
/// Removed the \n added at the end of each page here (potentially a breaking change!).
121-
/// Since every page can be retrieved individually, the client knows exactly
122-
/// where it begins and where it ends, therefore there is no benefit of
123-
/// introducing an artificial page separator that is not part of the pdf
124-
/// document per se
125-
return _pages!.fold<String>("", (pv, page) => "$pv${page._text}");
116+
/// Returning the entire text, concatenating all pages
117+
return _pages.fold<String>("", (pv, page) => "$pv${page._text}");
126118
}
127119

128120
/// Deletes the file related to this [PDFDoc].
@@ -165,7 +157,7 @@ class PDFPage {
165157
/// Gets the text of this page.
166158
/// The text retrieval is lazy. So the text of a page is only loaded when
167159
/// it is requested for the first time.
168-
Future<String?> get text async {
160+
Future<String> get text async {
169161
// Loading the text
170162
if (_text == null) {
171163
try {
@@ -178,7 +170,7 @@ class PDFPage {
178170
return Future.error(e);
179171
}
180172
}
181-
return _text;
173+
return _text!;
182174
}
183175

184176
/// Gets the page number.

0 commit comments

Comments
 (0)