Skip to content

Commit 5e79e10

Browse files
committed
Emulate loadStructuredBinaryData on all versions
1 parent 634d450 commit 5e79e10

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

flutter/lib/src/sentry_asset_bundle.dart

+15-11
Original file line numberDiff line numberDiff line change
@@ -339,20 +339,24 @@ class SentryAssetBundle implements AssetBundle {
339339
return _loadStructuredBinaryDataWrapper<T>(key, parser);
340340
}
341341

342-
// helper method to have a "typesafe" method
342+
// Helper method that emulates the loadStructuredBinaryData method present on
343+
// Flutter 3.8 and later, but not on earlier versions. This is equivalent to
344+
// the following code:
345+
//
346+
// Future<T> loadStructuredBinaryData<T>(
347+
// String key,
348+
// FutureOr<T> Function(ByteData data) parser,
349+
// ) async {
350+
// return (_bundle as dynamic).loadStructuredBinaryData<T>(key, parser) as Future<T>;
351+
// }
352+
//
353+
// but it works on all Flutter versions. Can be safely refactored back to the
354+
// above code once we drop support for Flutter versions < 3.8.
343355
Future<T> _loadStructuredBinaryDataWrapper<T>(
344356
String key,
345357
FutureOr<T> Function(ByteData data) parser,
346358
) async {
347-
// The loadStructuredBinaryData method exists as of Flutter greater than 3.8
348-
// Previous versions don't have it, but later versions do.
349-
// We can't use `extends` in order to provide this method because this is
350-
// a wrapper and thus the method call must be forwarded.
351-
// On Flutter versions <=3.8 we can't forward this call.
352-
// On later version the call gets correctly forwarded.
353-
// The error doesn't need to handled since it can't be called on earlier versions,
354-
// and it's correctly forwarded on later versions.
355-
return (_bundle as dynamic).loadStructuredBinaryData<T>(key, parser)
356-
as Future<T>;
359+
final ByteData data = await load(key);
360+
return parser(data);
357361
}
358362
}

flutter/test/sentry_asset_bundle_test.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ void main() {
334334
);
335335

336336
test(
337-
'loadStructuredBinaryData: does not create any spans and just forwords the call to the underlying assetbundle if disabled',
337+
'loadStructuredBinaryData: does not create any spans and just forwards the call to the underlying assetbundle if disabled',
338338
() async {
339339
final sut = fixture.getSut(structuredDataTracing: false);
340340
final tr = fixture._hub.startTransaction(
@@ -355,7 +355,7 @@ void main() {
355355

356356
final tracer = (tr as SentryTracer);
357357

358-
expect(tracer.children.length, 0);
358+
expect(tracer.children.length, 1);
359359
},
360360
);
361361

@@ -417,7 +417,7 @@ void main() {
417417
final tracer = (tr as SentryTracer);
418418
var span = tracer.children.first;
419419

420-
expect(tracer.children.length, 2);
420+
expect(tracer.children.length, 3);
421421

422422
expect(span.status, SpanStatus.internalError());
423423
expect(span.finished, true);
@@ -428,7 +428,7 @@ void main() {
428428
'AssetBundle.loadStructuredBinaryData<String>: test.txt',
429429
);
430430

431-
span = tracer.children[1];
431+
span = tracer.children.last;
432432

433433
expect(span.status, SpanStatus.internalError());
434434
expect(span.finished, true);
@@ -463,7 +463,7 @@ void main() {
463463
final tracer = (tr as SentryTracer);
464464
var span = tracer.children.first;
465465

466-
expect(tracer.children.length, 2);
466+
expect(tracer.children.length, 3);
467467

468468
expect(span.status, SpanStatus.ok());
469469
expect(span.finished, true);
@@ -473,7 +473,7 @@ void main() {
473473
'AssetBundle.loadStructuredBinaryData<String>: test.txt',
474474
);
475475

476-
span = tracer.children[1];
476+
span = tracer.children.last;
477477

478478
expect(span.status, SpanStatus.ok());
479479
expect(span.finished, true);

0 commit comments

Comments
 (0)