google / google/googleapis.dart
Throw a helpful error when `HOME` is not defined in environment
- Dominant language
- Dart
- Stars
- 419
- Forks
- 136
- Avg merge
- 1h 21m
- Merged PRs (30d)
- 5
Description
Hi :)
First please remember that Im relatively new in Dart/Flutter.
Im getting "Caught error: Null check operator used on a null value" after line where Im initialising "clientViaApplicationDefaultCredentials"
Im trying to get my youtube playlists but I probably making something wrong. Can you please take a look ? Im making exacly how it is in example code. Btw more examples would be great !
```dart
import 'dart:async';
import 'package:googleapis_auth/auth_io.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis/youtube/v3.dart';
GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: [
'email',
'https://www.googleapis.com/auth/youtube.readonly',
],
);
void main() {
runApp(
MaterialApp(
title: 'Google Sign In',
home: SignInDemo(),
),
);
}
class SignInDemo extends StatefulWidget {
@override
State createState() => SignInDemoState();
}
class SignInDemoState extends State {
GoogleSignInAccount? _currentUser;
String _contactText = '';
@override
void initState() {
super.initState();
_googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount? account) {
setState(() {
_currentUser = account;
});
});
_googleSignIn.signInSilently();
}
void printWrapped(String text) {
final pattern = RegExp('.{1,800}'); // 800 is the size of each chunk
pattern.allMatches(text).forEach((match) => print(match.group(0)));
}
Future _handleSignIn() async {
try {
await _googleSignIn.signIn();
} catch (error) {
print(error);
}
}
Future _handleSignOut() => _googleSignIn.disconnect();
Widget _buildBody() {
GoogleSignInAccount? user = _currentUser;
if (user != null) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ListTile(
title: Text(user.displayName ?? ''),
subtitle: Text(user.email),
),
const Text("Signed in successfully."),
Text(_contactText),
ElevatedButton(
child: const Text('SIGN OUT'),
onPressed: _handleSignOut,
),
ElevatedButton(
child: const Text('REFRESH'),
onPressed: () => _handleGetPlaylist(user),
),
],
);
} else {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
const Text("You are not currently signed in."),
ElevatedButton(
child: const Text('SIGN IN'),
onPressed: _handleSignIn,
),
],
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Google Sign In'),
),
body: ConstrainedBox(
constraints: const BoxConstraints.expand(),
child: _buildBody(),
));
}
_handleGetPlaylist(GoogleSignInAccount? user) async {
print("_handleGetPlaylist 161719393");
try {
print('Awaiting user order...');
final httpClient = await clientViaApplicationDefaultCredentials(
scopes: [YouTubeApi.youtubeReadonlyScope],
);
print("after clientViaApplicationDefaultCredentials 668601306");
// final client = http.Client();
YouTubeApi youtubeApi = YouTubeApi(httpClient);
print("after youtubeApi 183403194");
youtubeApi.playlists.list(["part=snippet"], mine: true).then((value) {
print("OKOK");
print(value);
}).catchError((err) {
print("Problem...");
print(err);
});
} catch (err) {
print('Caught error: $err');
}
}
}
```
OUTPUT:
I/flutter ( 2890): _handleGetPlaylist 161719393
I/flutter ( 2890): Awaiting user order...
I/flutter ( 2890): Caught error: Null check operator used on a null value
Contributor guide
Assessment
This issue has not been assessed yet.