mirror of https://github.com/flutter/samples.git
parent
e978f4dcc0
commit
16fa89a9b2
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,34 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:compass_app/common/utils/result.dart';
|
||||
import 'package:compass_app/features/results/business/model/destination.dart';
|
||||
import 'package:compass_app/features/results/data/destination_repository.dart';
|
||||
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
|
||||
/// Local implementation of DestinationRepository
|
||||
/// Uses data from assets folder
|
||||
class DestinationRepositoryLocal implements DestinationRepository {
|
||||
@override
|
||||
Future<Result<List<Destination>>> getDestinations() async {
|
||||
try {
|
||||
final localData = await _loadAsset();
|
||||
final list = _parse(localData);
|
||||
return Result.ok(list);
|
||||
} on Exception catch (error) {
|
||||
return Result.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _loadAsset() async {
|
||||
return await rootBundle.loadString('assets/destinations.json');
|
||||
}
|
||||
|
||||
List<Destination> _parse(String localData) {
|
||||
final parsed = (jsonDecode(localData) as List).cast<Map<String, dynamic>>();
|
||||
|
||||
return parsed
|
||||
.map<Destination>((json) => Destination.fromJson(json))
|
||||
.toList();
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ResultCard extends StatelessWidget {
|
||||
const ResultCard({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Placeholder();
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
|
||||
import 'package:compass_app/common/utils/result.dart';
|
||||
import 'package:compass_app/features/results/data/destination_repository_local.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('DestinationRepositoryLocal tests', () {
|
||||
// To load assets
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
final repository = DestinationRepositoryLocal();
|
||||
|
||||
test('should load and parse', () async {
|
||||
// Should load the json and parse it
|
||||
final result = await repository.getDestinations();
|
||||
expect(result, isA<Ok>());
|
||||
|
||||
// Check that the list is complete
|
||||
final list = result.asOk.value;
|
||||
expect(list.length, 137);
|
||||
|
||||
// Check first item
|
||||
final destination = list.first;
|
||||
expect(destination.name, 'Alaska');
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue