diff --git a/lib/ui/BUILD.gn b/lib/ui/BUILD.gn index 80b4f773f9bf2..6d6fef7278cbd 100644 --- a/lib/ui/BUILD.gn +++ b/lib/ui/BUILD.gn @@ -185,6 +185,7 @@ if (enable_unittests) { dart_main = "fixtures/ui_test.dart" fixtures = [ "fixtures/DashInNooglerHat.jpg", + "fixtures/DashInNooglerHat%20WithSpace.jpg", "fixtures/Horizontal.jpg", "fixtures/Horizontal.png", "fixtures/hello_loop_2.gif", diff --git a/lib/ui/fixtures/DashInNooglerHat%20WithSpace.jpg b/lib/ui/fixtures/DashInNooglerHat%20WithSpace.jpg new file mode 100644 index 0000000000000..488fdb4d5215c Binary files /dev/null and b/lib/ui/fixtures/DashInNooglerHat%20WithSpace.jpg differ diff --git a/lib/ui/painting.dart b/lib/ui/painting.dart index c0baaec260b79..79b027ac996c2 100644 --- a/lib/ui/painting.dart +++ b/lib/ui/painting.dart @@ -5674,9 +5674,14 @@ class ImmutableBuffer extends NativeFieldWrapperClass1 { /// /// Throws an [Exception] if the asset does not exist. static Future fromAsset(String assetKey) { + // The flutter tool converts all asset keys with spaces into URI + // encoded paths (replacing ' ' with '%20', for example). We perform + // the same encoding here so that users can load assets with the same + // key they have written in the pubspec. + final String encodedKey = Uri(path: Uri.encodeFull(assetKey)).path; final ImmutableBuffer instance = ImmutableBuffer._(0); return _futurize((_Callback callback) { - return instance._initFromAsset(assetKey, callback); + return instance._initFromAsset(encodedKey, callback); }).then((int length) => instance.._length = length); } diff --git a/testing/dart/assets_test.dart b/testing/dart/assets_test.dart index 7a2522c399fc5..64943c2179a91 100644 --- a/testing/dart/assets_test.dart +++ b/testing/dart/assets_test.dart @@ -27,6 +27,13 @@ void main() { expect(buffer.length == 354679, true); }); + test('Can load an asset with a space in the key', () async { + // This assets actual path is "fixtures/DashInNooglerHat%20WithSpace.jpg" + final ImmutableBuffer buffer = await ImmutableBuffer.fromAsset('DashInNooglerHat WithSpace.jpg'); + + expect(buffer.length == 354679, true); + }); + test('can dispose immutable buffer', () async { final ImmutableBuffer buffer = await ImmutableBuffer.fromAsset('DashInNooglerHat.jpg');