Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Emulate loadStructuredBinaryData on all versions
  • Loading branch information
arnemolland committed Feb 27, 2023
commit 5e79e107c60549cf3c90c7bf7b9efadac8e1589c
26 changes: 15 additions & 11 deletions flutter/lib/src/sentry_asset_bundle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,20 +339,24 @@ class SentryAssetBundle implements AssetBundle {
return _loadStructuredBinaryDataWrapper<T>(key, parser);
}

// helper method to have a "typesafe" method
// Helper method that emulates the loadStructuredBinaryData method present on
// Flutter 3.8 and later, but not on earlier versions. This is equivalent to
// the following code:
//
// Future<T> loadStructuredBinaryData<T>(
// String key,
// FutureOr<T> Function(ByteData data) parser,
// ) async {
// return (_bundle as dynamic).loadStructuredBinaryData<T>(key, parser) as Future<T>;
// }
//
// but it works on all Flutter versions. Can be safely refactored back to the
// above code once we drop support for Flutter versions < 3.8.
Future<T> _loadStructuredBinaryDataWrapper<T>(
String key,
FutureOr<T> Function(ByteData data) parser,
) async {
// The loadStructuredBinaryData method exists as of Flutter greater than 3.8
// Previous versions don't have it, but later versions do.
// We can't use `extends` in order to provide this method because this is
// a wrapper and thus the method call must be forwarded.
// On Flutter versions <=3.8 we can't forward this call.
// On later version the call gets correctly forwarded.
// The error doesn't need to handled since it can't be called on earlier versions,
// and it's correctly forwarded on later versions.
return (_bundle as dynamic).loadStructuredBinaryData<T>(key, parser)
as Future<T>;
final ByteData data = await load(key);
return parser(data);
}
}
12 changes: 6 additions & 6 deletions flutter/test/sentry_asset_bundle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ void main() {
);

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

final tracer = (tr as SentryTracer);

expect(tracer.children.length, 0);
expect(tracer.children.length, 1);
},
);

Expand Down Expand Up @@ -417,7 +417,7 @@ void main() {
final tracer = (tr as SentryTracer);
var span = tracer.children.first;

expect(tracer.children.length, 2);
expect(tracer.children.length, 3);

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

span = tracer.children[1];
span = tracer.children.last;

expect(span.status, SpanStatus.internalError());
expect(span.finished, true);
Expand Down Expand Up @@ -463,7 +463,7 @@ void main() {
final tracer = (tr as SentryTracer);
var span = tracer.children.first;

expect(tracer.children.length, 2);
expect(tracer.children.length, 3);

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

span = tracer.children[1];
span = tracer.children.last;

expect(span.status, SpanStatus.ok());
expect(span.finished, true);
Expand Down