Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate example
  • Loading branch information
stuartmorgan-g committed Feb 25, 2021
commit 2714bab6eb8c5a85fdf82cd9363c8346deeda6e5
37 changes: 19 additions & 18 deletions packages/google_sign_in/google_sign_in/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,31 @@ class SignInDemo extends StatefulWidget {
}

class SignInDemoState extends State<SignInDemo> {
GoogleSignInAccount _currentUser;
String _contactText;
GoogleSignInAccount? _currentUser;
String? _contactText;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super optional nits: default to empty String. 😝

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


@override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) {
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
setState(() {
_currentUser = account;
});
if (_currentUser != null) {
_handleGetContact();
_handleGetContact(_currentUser!);
}
});
_googleSignIn.signInSilently();
}

Future<void> _handleGetContact() async {
Future<void> _handleGetContact(GoogleSignInAccount user) async {
setState(() {
_contactText = "Loading contact info...";
});
final http.Response response = await http.get(
'https://people.googleapis.com/v1/people/me/connections'
'?requestMask.includeField=person.names',
headers: await _currentUser.authHeaders,
Uri.parse('https://people.googleapis.com/v1/people/me/connections'
'?requestMask.includeField=person.names'),
headers: await user.authHeaders,
);
if (response.statusCode != 200) {
setState(() {
Expand All @@ -68,7 +68,7 @@ class SignInDemoState extends State<SignInDemo> {
return;
}
final Map<String, dynamic> data = json.decode(response.body);
final String namedContact = _pickFirstNamedContact(data);
final String? namedContact = _pickFirstNamedContact(data);
setState(() {
if (namedContact != null) {
_contactText = "I see you know $namedContact!";
Expand All @@ -78,14 +78,14 @@ class SignInDemoState extends State<SignInDemo> {
});
}

String _pickFirstNamedContact(Map<String, dynamic> data) {
final List<dynamic> connections = data['connections'];
final Map<String, dynamic> contact = connections?.firstWhere(
String? _pickFirstNamedContact(Map<String, dynamic> data) {
final List<dynamic>? connections = data['connections'];
final Map<String, dynamic>? contact = connections?.firstWhere(
(dynamic contact) => contact['names'] != null,
orElse: () => null,
);
if (contact != null) {
final Map<String, dynamic> name = contact['names'].firstWhere(
final Map<String, dynamic>? name = contact['names'].firstWhere(
(dynamic name) => name['displayName'] != null,
orElse: () => null,
);
Expand All @@ -107,16 +107,17 @@ class SignInDemoState extends State<SignInDemo> {
Future<void> _handleSignOut() => _googleSignIn.disconnect();

Widget _buildBody() {
if (_currentUser != null) {
GoogleSignInAccount? user = _currentUser;
if (user != null) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
ListTile(
leading: GoogleUserCircleAvatar(
identity: _currentUser,
identity: user,
),
title: Text(_currentUser.displayName ?? ''),
subtitle: Text(_currentUser.email ?? ''),
title: Text(user.displayName ?? ''),
subtitle: Text(user.email),
),
const Text("Signed in successfully."),
Text(_contactText ?? ''),
Expand All @@ -126,7 +127,7 @@ class SignInDemoState extends State<SignInDemo> {
),
ElevatedButton(
child: const Text('REFRESH'),
onPressed: _handleGetContact,
onPressed: () => _handleGetContact(user),
),
],
);
Expand Down
6 changes: 3 additions & 3 deletions packages/google_sign_in/google_sign_in/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
http: ^0.12.0
http: ^0.13.0

dev_dependencies:
pedantic: ^1.8.0
pedantic: ^1.10.0
integration_test:
path: ../../../integration_test
flutter_driver:
Expand All @@ -24,5 +24,5 @@ flutter:
uses-material-design: true

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
sdk: ">=2.12.0-259.9.beta <3.0.0"
flutter: ">=1.12.13+hotfix.4"