[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
- Removes DebugGoRouteInformation.

View File

@ -2,11 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// 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: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
// location.
final Map<String, dynamic> _families = const JsonDecoder().convert('''
{
"f1": {
"name": "Doe",
"people": {
"p1": {
"name": "Jane",
"age": 23
},
"p2": {
"name": "John",
"age": 6
}
}
},
"f2": {
"name": "Wong",
"people": {
"p1": {
"name": "June",
"age": 51
},
"p2": {
"name": "Xin",
"age": 44
}
}
}
/// Family data class.
class Family {
/// Create a family.
const Family({required this.name, required this.people});
/// The last name of the family.
final String name;
/// The people in the family.
final Map<String, Person> people;
}
''');
/// 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());
@ -110,11 +115,11 @@ class HomeScreen extends StatelessWidget {
),
body: ListView(
children: <Widget>[
for (final String fid in _families.keys)
for (final MapEntry<String, Family> entry in _families.entries)
ListTile(
title: Text(_families[fid]['name']),
title: Text(entry.value.name),
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
Widget build(BuildContext context) {
final Map<String, dynamic> people =
_families[fid]['people'] as Map<String, dynamic>;
final Map<String, Person> people = _families[fid]!.people;
return Scaffold(
appBar: AppBar(title: Text(_families[fid]['name'])),
appBar: AppBar(title: Text(_families[fid]!.name)),
body: ListView(
children: <Widget>[
for (final String pid in people.keys)
for (final MapEntry<String, Person> entry in people.entries)
ListTile(
title: Text(people[pid]['name']),
title: Text(entry.value.name),
onTap: () => context.go(context.namedLocation(
'person',
params: <String, String>{'fid': fid, 'pid': pid},
params: <String, String>{'fid': fid, 'pid': entry.key},
queryParams: <String, String>{'qid': 'quid'},
)),
),
@ -167,12 +171,11 @@ class PersonScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Map<String, dynamic> family = _families[fid];
final Map<String, dynamic> person = family['people'][pid];
final Family family = _families[fid]!;
final Person person = family.people[pid]!;
return Scaffold(
appBar: AppBar(title: Text(person['name'])),
body: Text(
'${person['name']} ${family['name']} is ${person['age']} years old'),
appBar: AppBar(title: Text(person.name)),
body: Text('${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
// 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:go_router/go_router.dart';
final Map<String, dynamic> _families = const JsonDecoder().convert('''
{
"f1": {
"name": "Doe",
"people": {
"p1": {
"name": "Jane",
"age": 23
},
"p2": {
"name": "John",
"age": 6
}
}
},
"f2": {
"name": "Wong",
"people": {
"p1": {
"name": "June",
"age": 51
},
"p2": {
"name": "Xin",
"age": 44
}
}
}
/// Family data class.
class Family {
/// Create a family.
const Family({required this.name, required this.people});
/// The last name of the family.
final String name;
/// The people in the family.
final Map<String, Person> people;
}
''');
/// 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());
@ -91,11 +96,11 @@ class HomeScreen extends StatelessWidget {
appBar: AppBar(title: const Text(App.title)),
body: ListView(
children: <Widget>[
for (final String fid in _families.keys)
for (final MapEntry<String, Family> entry in _families.entries)
ListTile(
title: Text(_families[fid]['name']),
onTap: () => context
.goNamed('family', extra: <String, String>{'fid': fid}),
title: Text(entry.value.name),
onTap: () => context.goNamed('family',
extra: <String, String>{'fid': entry.key}),
)
],
),
@ -112,15 +117,14 @@ class FamilyScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Map<String, dynamic> people =
_families[fid]['people'] as Map<String, dynamic>;
final Map<String, Person> people = _families[fid]!.people;
return Scaffold(
appBar: AppBar(title: Text(_families[fid]['name'])),
appBar: AppBar(title: Text(_families[fid]!.name)),
body: ListView(
children: <Widget>[
for (final dynamic p in people.values)
for (final Person p in people.values)
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
// 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: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.
final Map<String, dynamic> _families = const JsonDecoder().convert('''
{
"f1": {
"name": "Doe",
"people": {
"p1": {
"name": "Jane",
"age": 23
},
"p2": {
"name": "John",
"age": 6
}
}
},
"f2": {
"name": "Wong",
"people": {
"p1": {
"name": "June",
"age": 51
},
"p2": {
"name": "Xin",
"age": 44
}
}
}
/// Family data class.
class Family {
/// Create a family.
const Family({required this.name, required this.people});
/// The last name of the family.
final String name;
/// The people in the family.
final Map<String, Person> people;
}
''');
/// 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());
@ -102,10 +107,10 @@ class HomeScreen extends StatelessWidget {
),
body: ListView(
children: <Widget>[
for (final String fid in _families.keys)
for (final MapEntry<String, Family> entry in _families.entries)
ListTile(
title: Text(_families[fid]['name']),
onTap: () => context.go('/family/$fid'),
title: Text(entry.value.name),
onTap: () => context.go('/family/${entry.key}'),
)
],
),
@ -128,9 +133,10 @@ class FamilyScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Map<String, String> newQueries;
final List<String> names = _families[fid]['people']
final List<String> names = _families[fid]!
.people
.values
.map<String>((dynamic p) => p['name'] as String)
.map<String>((Person p) => p.name)
.toList();
names.sort();
if (asc) {
@ -140,7 +146,7 @@ class FamilyScreen extends StatelessWidget {
}
return Scaffold(
appBar: AppBar(
title: Text(_families[fid]['name']),
title: Text(_families[fid]!.name),
actions: <Widget>[
IconButton(
onPressed: () => context.goNamed('family',