Skip to content

Commit

Permalink
refactor: Simplify how images.dart decodes images (#2293)
Browse files Browse the repository at this point in the history
* refactor: simplify how images.dart decodes images

* fix tests
  • Loading branch information
erickzanardo committed Jan 26, 2023
1 parent 74969a5 commit b492542
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
11 changes: 3 additions & 8 deletions packages/flame/lib/src/cache/images.dart
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert' show base64, json;
import 'dart:ui';

import 'package:flame/src/flame.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/services.dart';

class Images {
Expand Down Expand Up @@ -155,19 +156,13 @@ class Images {
Future<Image> _fetchFromBase64(String base64Data) {
final data = base64Data.substring(base64Data.indexOf(',') + 1);
final bytes = base64.decode(data);
return _loadBytes(bytes);
return decodeImageFromList(bytes);
}

Future<Image> _fetchToMemory(String name) async {
final data = await Flame.bundle.load('$_prefix$name');
final bytes = Uint8List.view(data.buffer);
return _loadBytes(bytes);
}

Future<Image> _loadBytes(Uint8List bytes) {
final completer = Completer<Image>();
decodeImageFromList(bytes, completer.complete);
return completer.future;
return decodeImageFromList(bytes);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/flame/test/cache/images_test.dart
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
// A simple 1x1 pixel encoded as base64 - just so that we have something to
// load into the Images cache.
const pixel =
Expand Down

0 comments on commit b492542

Please sign in to comment.