This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Japanese documents for gorouter.dev (#265)
- Loading branch information
1 parent
b9ac4e0
commit 0802fe8
Showing
22 changed files
with
2,487 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# 非同期データ | ||
|
||
非同期に取得したデータを元に画面を生成したい場合は、パラメータを画面側に渡してその先でデータの取得を行います。 | ||
|
||
```dart | ||
late final _router = GoRouter( | ||
routes: [ | ||
GoRoute( | ||
path: '/', | ||
builder: (context, state) => const HomeScreenWithAsync(), | ||
routes: [ | ||
GoRoute( | ||
path: 'family/:fid', | ||
builder: (context, state) => FamilyScreenWithAsync( | ||
fid: state.params['fid']!, | ||
), | ||
routes: [ | ||
GoRoute( | ||
path: 'person/:pid', | ||
builder: (context, state) => PersonScreenWithAsync( | ||
fid: state.params['fid']!, | ||
pid: state.params['pid']!, | ||
), | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
); | ||
``` | ||
|
||
データの取得はどのような方法でも構いません。 | ||
例えば、以下のサンプルコードは[リポジトリ・パターン](https://martinfowler.com/eaaCatalog/repository.html)と | ||
[Flutter の FutureBuilder](https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html) を使用してデータのロードと表示を行っています。 | ||
|
||
```dart | ||
class FamilyScreenWithAsync extends StatefulWidget { | ||
const FamilyScreenWithAsync({required this.fid, Key? key}) : super(key: key); | ||
final String fid; | ||
@override | ||
State<FamilyScreenWithAsync> createState() => _FamilyScreenWithAsyncState(); | ||
} | ||
class _FamilyScreenWithAsyncState extends State<FamilyScreenWithAsync> { | ||
Future<Family>? _future; | ||
Family? _family; | ||
@override | ||
void initState() { | ||
super.initState(); | ||
fetch(); | ||
} | ||
@override | ||
void didUpdateWidget(covariant FamilyScreenWithAsync oldWidget) { | ||
super.didUpdateWidget(oldWidget); | ||
// キャッシュデータを更新 | ||
if (oldWidget.fid != widget.fid) fetch(); | ||
} | ||
void fetch() { | ||
_family = null; | ||
_future = App.repo.getFamily(widget.fid); | ||
_future!.then( | ||
(family) => setState(() => _family = family), // AppBar を更新 | ||
); | ||
} | ||
@override | ||
Widget build(BuildContext context) => Scaffold( | ||
appBar: AppBar(title: Text(_family?.name ?? 'loading...')), | ||
body: FutureBuilder<Family>( | ||
future: _future, | ||
builder: (context, snapshot) { | ||
if (snapshot.connectionState == ConnectionState.waiting) { | ||
return const Center(child: CircularProgressIndicator()); | ||
} | ||
if (snapshot.hasError) return SnapshotError(snapshot.error!); | ||
assert(snapshot.hasData); | ||
return ListView( | ||
children: [ | ||
for (final p in _family!.people) | ||
ListTile( | ||
title: Text(p.name), | ||
onTap: () => | ||
context.go('/family/${_family!.id}/person/${p.id}'), | ||
), | ||
], | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
``` | ||
|
||
データ取得中はロードインジケーターを表示し、取得が失敗した場合はエラー内容を表示します。 | ||
|
||
 | ||
|
||
このサンプルコードのフルバージョンは[こちら](https://github.com/csells/go_router/blob/master/example/lib/async_data.dart)でご覧いただけます。 |
Oops, something went wrong.