-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from kyushu-silicon-valley/home_backend
- Loading branch information
Showing
5 changed files
with
240 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:calorie_go_flutter/pages/home/home_page_state.dart'; | ||
import 'package:calorie_go_flutter/repositories/monster_repository.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
part 'home_page_controller.g.dart'; | ||
|
||
@riverpod | ||
class HomePageController extends _$HomePageController { | ||
@override | ||
HomePageState build() { | ||
WidgetsBinding.instance.addPostFrameCallback((_) { | ||
getMonster(); | ||
}); | ||
return const HomePageState(); | ||
} | ||
|
||
Future<void> getMonster() async { | ||
try { | ||
final monster = await MonsterRepository().getMonster(); | ||
if(monster == null) throw Exception('モンスターが見つかりませんでした'); | ||
state = state.copyWith(monster: AsyncValue.data(monster)); | ||
} catch (e) { | ||
state = state.copyWith(errorMessage: '$e'); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
calorie_go_flutter/lib/pages/home/home_page_controller.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:calorie_go_client/calorie_go_client.dart'; | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
|
||
part 'home_page_state.freezed.dart'; | ||
|
||
@freezed | ||
class HomePageState with _$HomePageState { | ||
const factory HomePageState({ | ||
@Default(AsyncValue<Monster>.loading()) AsyncValue<Monster> monster, | ||
String? errorMessage, | ||
}) = _HomePageState; | ||
} |
153 changes: 153 additions & 0 deletions
153
calorie_go_flutter/lib/pages/home/home_page_state.freezed.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// coverage:ignore-file | ||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
// ignore_for_file: type=lint | ||
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark | ||
|
||
part of 'home_page_state.dart'; | ||
|
||
// ************************************************************************** | ||
// FreezedGenerator | ||
// ************************************************************************** | ||
|
||
T _$identity<T>(T value) => value; | ||
|
||
final _privateConstructorUsedError = UnsupportedError( | ||
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models'); | ||
|
||
/// @nodoc | ||
mixin _$HomePageState { | ||
AsyncValue<Monster> get monster => throw _privateConstructorUsedError; | ||
String? get errorMessage => throw _privateConstructorUsedError; | ||
|
||
@JsonKey(ignore: true) | ||
$HomePageStateCopyWith<HomePageState> get copyWith => | ||
throw _privateConstructorUsedError; | ||
} | ||
|
||
/// @nodoc | ||
abstract class $HomePageStateCopyWith<$Res> { | ||
factory $HomePageStateCopyWith( | ||
HomePageState value, $Res Function(HomePageState) then) = | ||
_$HomePageStateCopyWithImpl<$Res, HomePageState>; | ||
@useResult | ||
$Res call({AsyncValue<Monster> monster, String? errorMessage}); | ||
} | ||
|
||
/// @nodoc | ||
class _$HomePageStateCopyWithImpl<$Res, $Val extends HomePageState> | ||
implements $HomePageStateCopyWith<$Res> { | ||
_$HomePageStateCopyWithImpl(this._value, this._then); | ||
|
||
// ignore: unused_field | ||
final $Val _value; | ||
// ignore: unused_field | ||
final $Res Function($Val) _then; | ||
|
||
@pragma('vm:prefer-inline') | ||
@override | ||
$Res call({ | ||
Object? monster = null, | ||
Object? errorMessage = freezed, | ||
}) { | ||
return _then(_value.copyWith( | ||
monster: null == monster | ||
? _value.monster | ||
: monster // ignore: cast_nullable_to_non_nullable | ||
as AsyncValue<Monster>, | ||
errorMessage: freezed == errorMessage | ||
? _value.errorMessage | ||
: errorMessage // ignore: cast_nullable_to_non_nullable | ||
as String?, | ||
) as $Val); | ||
} | ||
} | ||
|
||
/// @nodoc | ||
abstract class _$$HomePageStateImplCopyWith<$Res> | ||
implements $HomePageStateCopyWith<$Res> { | ||
factory _$$HomePageStateImplCopyWith( | ||
_$HomePageStateImpl value, $Res Function(_$HomePageStateImpl) then) = | ||
__$$HomePageStateImplCopyWithImpl<$Res>; | ||
@override | ||
@useResult | ||
$Res call({AsyncValue<Monster> monster, String? errorMessage}); | ||
} | ||
|
||
/// @nodoc | ||
class __$$HomePageStateImplCopyWithImpl<$Res> | ||
extends _$HomePageStateCopyWithImpl<$Res, _$HomePageStateImpl> | ||
implements _$$HomePageStateImplCopyWith<$Res> { | ||
__$$HomePageStateImplCopyWithImpl( | ||
_$HomePageStateImpl _value, $Res Function(_$HomePageStateImpl) _then) | ||
: super(_value, _then); | ||
|
||
@pragma('vm:prefer-inline') | ||
@override | ||
$Res call({ | ||
Object? monster = null, | ||
Object? errorMessage = freezed, | ||
}) { | ||
return _then(_$HomePageStateImpl( | ||
monster: null == monster | ||
? _value.monster | ||
: monster // ignore: cast_nullable_to_non_nullable | ||
as AsyncValue<Monster>, | ||
errorMessage: freezed == errorMessage | ||
? _value.errorMessage | ||
: errorMessage // ignore: cast_nullable_to_non_nullable | ||
as String?, | ||
)); | ||
} | ||
} | ||
|
||
/// @nodoc | ||
class _$HomePageStateImpl implements _HomePageState { | ||
const _$HomePageStateImpl( | ||
{this.monster = const AsyncValue<Monster>.loading(), this.errorMessage}); | ||
|
||
@override | ||
@JsonKey() | ||
final AsyncValue<Monster> monster; | ||
@override | ||
final String? errorMessage; | ||
|
||
@override | ||
String toString() { | ||
return 'HomePageState(monster: $monster, errorMessage: $errorMessage)'; | ||
} | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
return identical(this, other) || | ||
(other.runtimeType == runtimeType && | ||
other is _$HomePageStateImpl && | ||
(identical(other.monster, monster) || other.monster == monster) && | ||
(identical(other.errorMessage, errorMessage) || | ||
other.errorMessage == errorMessage)); | ||
} | ||
|
||
@override | ||
int get hashCode => Object.hash(runtimeType, monster, errorMessage); | ||
|
||
@JsonKey(ignore: true) | ||
@override | ||
@pragma('vm:prefer-inline') | ||
_$$HomePageStateImplCopyWith<_$HomePageStateImpl> get copyWith => | ||
__$$HomePageStateImplCopyWithImpl<_$HomePageStateImpl>(this, _$identity); | ||
} | ||
|
||
abstract class _HomePageState implements HomePageState { | ||
const factory _HomePageState( | ||
{final AsyncValue<Monster> monster, | ||
final String? errorMessage}) = _$HomePageStateImpl; | ||
|
||
@override | ||
AsyncValue<Monster> get monster; | ||
@override | ||
String? get errorMessage; | ||
@override | ||
@JsonKey(ignore: true) | ||
_$$HomePageStateImplCopyWith<_$HomePageStateImpl> get copyWith => | ||
throw _privateConstructorUsedError; | ||
} |