first bang implementation
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:bang_navigator/core/http_error_handler.dart';
|
||||
import 'package:bang_navigator/features/bangs/data/models/bang.dart';
|
||||
import 'package:exceptions/exceptions.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'bang_source.g.dart';
|
||||
|
||||
@Riverpod()
|
||||
class BangSource extends _$BangSource {
|
||||
late http.Client _client;
|
||||
|
||||
@override
|
||||
void build() {
|
||||
_client = http.Client();
|
||||
}
|
||||
|
||||
Future<Result<List<Bang>>> getBangs(Uri url, BangGroup? group) async {
|
||||
return Result.fromAsync(
|
||||
() async {
|
||||
final response = await _client.get(url);
|
||||
return await compute(
|
||||
(args) => jsonDecode(utf8.decode(args[0])) as List,
|
||||
[response.bodyBytes],
|
||||
).then(
|
||||
(json) => json.map((e) {
|
||||
var bang = Bang.fromJson(e as Map<String, dynamic>);
|
||||
|
||||
if (group != null) {
|
||||
bang = bang.copyWith.group(group);
|
||||
}
|
||||
|
||||
return bang;
|
||||
}).toList(),
|
||||
);
|
||||
},
|
||||
exceptionHandler: handleHttpError,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'bang_source.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$bangSourceHash() => r'6ca70e8be16a0ae104fa6b739e3c0f2c59eab565';
|
||||
|
||||
/// See also [BangSource].
|
||||
@ProviderFor(BangSource)
|
||||
final bangSourceProvider =
|
||||
AutoDisposeNotifierProvider<BangSource, void>.internal(
|
||||
BangSource.new,
|
||||
name: r'bangSourceProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$bangSourceHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$BangSource = AutoDisposeNotifier<void>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
Reference in New Issue
Block a user