[go_router] Removes dynamic calls in examples. (#2714)

This commit is contained in:
chunhtai
2022-10-18 12:56:05 -07:00
committed by GitHub
parent ec78204d73
commit d38982ce60
4 changed files with 148 additions and 131 deletions

View File

@ -1,3 +1,7 @@
## NEXT
- Removes dynamic calls in examples.
## 5.1.1 ## 5.1.1
- Removes DebugGoRouteInformation. - Removes DebugGoRouteInformation.

View File

@ -2,11 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// TODO(goderbauer): Refactor the examples to remove this ignore, https://github.com/flutter/flutter/issues/110210
// ignore_for_file: avoid_dynamic_calls
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
@ -18,36 +13,46 @@ import 'package:go_router/go_router.dart';
// then be used in context.namedLocation to be translate back to the actual URL // then be used in context.namedLocation to be translate back to the actual URL
// location. // location.
final Map<String, dynamic> _families = const JsonDecoder().convert(''' /// Family data class.
{ class Family {
"f1": { /// Create a family.
"name": "Doe", const Family({required this.name, required this.people});
"people": {
"p1": { /// The last name of the family.
"name": "Jane", final String name;
"age": 23
}, /// The people in the family.
"p2": { final Map<String, Person> people;
"name": "John",
"age": 6
}
}
},
"f2": {
"name": "Wong",
"people": {
"p1": {
"name": "June",
"age": 51
},
"p2": {
"name": "Xin",
"age": 44
}
}
}
} }
''');
/// Person data class.
class Person {
/// Creates a person.
const Person({required this.name, required this.age});
/// The first name of the person.
final String name;
/// The age of the person.
final int age;
}
const Map<String, Family> _families = <String, Family>{
'f1': Family(
name: 'Doe',
people: <String, Person>{
'p1': Person(name: 'Jane', age: 23),
'p2': Person(name: 'John', age: 6),
},
),
'f2': Family(
name: 'Wong',
people: <String, Person>{
'p1': Person(name: 'June', age: 51),
'p2': Person(name: 'Xin', age: 44),
},
),
};
void main() => runApp(App()); void main() => runApp(App());
@ -110,11 +115,11 @@ class HomeScreen extends StatelessWidget {
), ),
body: ListView( body: ListView(
children: <Widget>[ children: <Widget>[
for (final String fid in _families.keys) for (final MapEntry<String, Family> entry in _families.entries)
ListTile( ListTile(
title: Text(_families[fid]['name']), title: Text(entry.value.name),
onTap: () => context.go(context.namedLocation('family', onTap: () => context.go(context.namedLocation('family',
params: <String, String>{'fid': fid})), params: <String, String>{'fid': entry.key})),
) )
], ],
), ),
@ -132,18 +137,17 @@ class FamilyScreen extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final Map<String, dynamic> people = final Map<String, Person> people = _families[fid]!.people;
_families[fid]['people'] as Map<String, dynamic>;
return Scaffold( return Scaffold(
appBar: AppBar(title: Text(_families[fid]['name'])), appBar: AppBar(title: Text(_families[fid]!.name)),
body: ListView( body: ListView(
children: <Widget>[ children: <Widget>[
for (final String pid in people.keys) for (final MapEntry<String, Person> entry in people.entries)
ListTile( ListTile(
title: Text(people[pid]['name']), title: Text(entry.value.name),
onTap: () => context.go(context.namedLocation( onTap: () => context.go(context.namedLocation(
'person', 'person',
params: <String, String>{'fid': fid, 'pid': pid}, params: <String, String>{'fid': fid, 'pid': entry.key},
queryParams: <String, String>{'qid': 'quid'}, queryParams: <String, String>{'qid': 'quid'},
)), )),
), ),
@ -167,12 +171,11 @@ class PersonScreen extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final Map<String, dynamic> family = _families[fid]; final Family family = _families[fid]!;
final Map<String, dynamic> person = family['people'][pid]; final Person person = family.people[pid]!;
return Scaffold( return Scaffold(
appBar: AppBar(title: Text(person['name'])), appBar: AppBar(title: Text(person.name)),
body: Text( body: Text('${person.name} ${family.name} is ${person.age} years old'),
'${person['name']} ${family['name']} is ${person['age']} years old'),
); );
} }
} }

View File

@ -2,44 +2,49 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// TODO(goderbauer): Refactor the examples to remove this ignore, https://github.com/flutter/flutter/issues/110210
// ignore_for_file: avoid_dynamic_calls
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
final Map<String, dynamic> _families = const JsonDecoder().convert(''' /// Family data class.
{ class Family {
"f1": { /// Create a family.
"name": "Doe", const Family({required this.name, required this.people});
"people": {
"p1": { /// The last name of the family.
"name": "Jane", final String name;
"age": 23
}, /// The people in the family.
"p2": { final Map<String, Person> people;
"name": "John",
"age": 6
}
}
},
"f2": {
"name": "Wong",
"people": {
"p1": {
"name": "June",
"age": 51
},
"p2": {
"name": "Xin",
"age": 44
}
}
}
} }
''');
/// Person data class.
class Person {
/// Creates a person.
const Person({required this.name, required this.age});
/// The first name of the person.
final String name;
/// The age of the person.
final int age;
}
const Map<String, Family> _families = <String, Family>{
'f1': Family(
name: 'Doe',
people: <String, Person>{
'p1': Person(name: 'Jane', age: 23),
'p2': Person(name: 'John', age: 6),
},
),
'f2': Family(
name: 'Wong',
people: <String, Person>{
'p1': Person(name: 'June', age: 51),
'p2': Person(name: 'Xin', age: 44),
},
),
};
void main() => runApp(App()); void main() => runApp(App());
@ -91,11 +96,11 @@ class HomeScreen extends StatelessWidget {
appBar: AppBar(title: const Text(App.title)), appBar: AppBar(title: const Text(App.title)),
body: ListView( body: ListView(
children: <Widget>[ children: <Widget>[
for (final String fid in _families.keys) for (final MapEntry<String, Family> entry in _families.entries)
ListTile( ListTile(
title: Text(_families[fid]['name']), title: Text(entry.value.name),
onTap: () => context onTap: () => context.goNamed('family',
.goNamed('family', extra: <String, String>{'fid': fid}), extra: <String, String>{'fid': entry.key}),
) )
], ],
), ),
@ -112,15 +117,14 @@ class FamilyScreen extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final Map<String, dynamic> people = final Map<String, Person> people = _families[fid]!.people;
_families[fid]['people'] as Map<String, dynamic>;
return Scaffold( return Scaffold(
appBar: AppBar(title: Text(_families[fid]['name'])), appBar: AppBar(title: Text(_families[fid]!.name)),
body: ListView( body: ListView(
children: <Widget>[ children: <Widget>[
for (final dynamic p in people.values) for (final Person p in people.values)
ListTile( ListTile(
title: Text(p['name']), title: Text(p.name),
), ),
], ],
), ),

View File

@ -2,11 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// TODO(goderbauer): Refactor the examples to remove this ignore, https://github.com/flutter/flutter/issues/110210
// ignore_for_file: avoid_dynamic_calls
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
@ -18,36 +13,46 @@ import 'package:go_router/go_router.dart';
// //
// The query parameters are automatically stored in GoRouterState.queryParams. // The query parameters are automatically stored in GoRouterState.queryParams.
final Map<String, dynamic> _families = const JsonDecoder().convert(''' /// Family data class.
{ class Family {
"f1": { /// Create a family.
"name": "Doe", const Family({required this.name, required this.people});
"people": {
"p1": { /// The last name of the family.
"name": "Jane", final String name;
"age": 23
}, /// The people in the family.
"p2": { final Map<String, Person> people;
"name": "John",
"age": 6
}
}
},
"f2": {
"name": "Wong",
"people": {
"p1": {
"name": "June",
"age": 51
},
"p2": {
"name": "Xin",
"age": 44
}
}
}
} }
''');
/// Person data class.
class Person {
/// Creates a person.
const Person({required this.name, required this.age});
/// The first name of the person.
final String name;
/// The age of the person.
final int age;
}
const Map<String, Family> _families = <String, Family>{
'f1': Family(
name: 'Doe',
people: <String, Person>{
'p1': Person(name: 'Jane', age: 23),
'p2': Person(name: 'John', age: 6),
},
),
'f2': Family(
name: 'Wong',
people: <String, Person>{
'p1': Person(name: 'June', age: 51),
'p2': Person(name: 'Xin', age: 44),
},
),
};
void main() => runApp(App()); void main() => runApp(App());
@ -102,10 +107,10 @@ class HomeScreen extends StatelessWidget {
), ),
body: ListView( body: ListView(
children: <Widget>[ children: <Widget>[
for (final String fid in _families.keys) for (final MapEntry<String, Family> entry in _families.entries)
ListTile( ListTile(
title: Text(_families[fid]['name']), title: Text(entry.value.name),
onTap: () => context.go('/family/$fid'), onTap: () => context.go('/family/${entry.key}'),
) )
], ],
), ),
@ -128,9 +133,10 @@ class FamilyScreen extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final Map<String, String> newQueries; final Map<String, String> newQueries;
final List<String> names = _families[fid]['people'] final List<String> names = _families[fid]!
.people
.values .values
.map<String>((dynamic p) => p['name'] as String) .map<String>((Person p) => p.name)
.toList(); .toList();
names.sort(); names.sort();
if (asc) { if (asc) {
@ -140,7 +146,7 @@ class FamilyScreen extends StatelessWidget {
} }
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(_families[fid]['name']), title: Text(_families[fid]!.name),
actions: <Widget>[ actions: <Widget>[
IconButton( IconButton(
onPressed: () => context.goNamed('family', onPressed: () => context.goNamed('family',