diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 49da632b2c..55d4e6d06c 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## NEXT + +- Removes dynamic calls in examples. + ## 5.1.1 - Removes DebugGoRouteInformation. diff --git a/packages/go_router/example/lib/named_routes.dart b/packages/go_router/example/lib/named_routes.dart index 237581faf1..f32960283b 100644 --- a/packages/go_router/example/lib/named_routes.dart +++ b/packages/go_router/example/lib/named_routes.dart @@ -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 _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 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 _families = { + 'f1': Family( + name: 'Doe', + people: { + 'p1': Person(name: 'Jane', age: 23), + 'p2': Person(name: 'John', age: 6), + }, + ), + 'f2': Family( + name: 'Wong', + people: { + '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: [ - for (final String fid in _families.keys) + for (final MapEntry entry in _families.entries) ListTile( - title: Text(_families[fid]['name']), + title: Text(entry.value.name), onTap: () => context.go(context.namedLocation('family', - params: {'fid': fid})), + params: {'fid': entry.key})), ) ], ), @@ -132,18 +137,17 @@ class FamilyScreen extends StatelessWidget { @override Widget build(BuildContext context) { - final Map people = - _families[fid]['people'] as Map; + final Map people = _families[fid]!.people; return Scaffold( - appBar: AppBar(title: Text(_families[fid]['name'])), + appBar: AppBar(title: Text(_families[fid]!.name)), body: ListView( children: [ - for (final String pid in people.keys) + for (final MapEntry entry in people.entries) ListTile( - title: Text(people[pid]['name']), + title: Text(entry.value.name), onTap: () => context.go(context.namedLocation( 'person', - params: {'fid': fid, 'pid': pid}, + params: {'fid': fid, 'pid': entry.key}, queryParams: {'qid': 'quid'}, )), ), @@ -167,12 +171,11 @@ class PersonScreen extends StatelessWidget { @override Widget build(BuildContext context) { - final Map family = _families[fid]; - final Map 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'), ); } } diff --git a/packages/go_router/example/lib/others/extra_param.dart b/packages/go_router/example/lib/others/extra_param.dart index a4d40e9647..2562b89a78 100644 --- a/packages/go_router/example/lib/others/extra_param.dart +++ b/packages/go_router/example/lib/others/extra_param.dart @@ -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 _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 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 _families = { + 'f1': Family( + name: 'Doe', + people: { + 'p1': Person(name: 'Jane', age: 23), + 'p2': Person(name: 'John', age: 6), + }, + ), + 'f2': Family( + name: 'Wong', + people: { + '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: [ - for (final String fid in _families.keys) + for (final MapEntry entry in _families.entries) ListTile( - title: Text(_families[fid]['name']), - onTap: () => context - .goNamed('family', extra: {'fid': fid}), + title: Text(entry.value.name), + onTap: () => context.goNamed('family', + extra: {'fid': entry.key}), ) ], ), @@ -112,15 +117,14 @@ class FamilyScreen extends StatelessWidget { @override Widget build(BuildContext context) { - final Map people = - _families[fid]['people'] as Map; + final Map people = _families[fid]!.people; return Scaffold( - appBar: AppBar(title: Text(_families[fid]['name'])), + appBar: AppBar(title: Text(_families[fid]!.name)), body: ListView( children: [ - for (final dynamic p in people.values) + for (final Person p in people.values) ListTile( - title: Text(p['name']), + title: Text(p.name), ), ], ), diff --git a/packages/go_router/example/lib/path_and_query_parameters.dart b/packages/go_router/example/lib/path_and_query_parameters.dart index 7e52baf68d..5b075875a4 100755 --- a/packages/go_router/example/lib/path_and_query_parameters.dart +++ b/packages/go_router/example/lib/path_and_query_parameters.dart @@ -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 _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 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 _families = { + 'f1': Family( + name: 'Doe', + people: { + 'p1': Person(name: 'Jane', age: 23), + 'p2': Person(name: 'John', age: 6), + }, + ), + 'f2': Family( + name: 'Wong', + people: { + '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: [ - for (final String fid in _families.keys) + for (final MapEntry 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 newQueries; - final List names = _families[fid]['people'] + final List names = _families[fid]! + .people .values - .map((dynamic p) => p['name'] as String) + .map((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: [ IconButton( onPressed: () => context.goNamed('family',