Created
July 4, 2022 20:01
-
-
Save danahartweg/5a5c3e25da6b17caa54fe9489b79cf17 to your computer and use it in GitHub Desktop.
User view - Unit testing Flutter GraphQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserView extends HookWidget { | |
final String _userId; | |
UserView(this._userId); | |
@override | |
Widget build(BuildContext context) { | |
final query = useQuery$UserById( | |
Options$Query$UserById( | |
variables: Variables$Query$UserById( | |
id: _userId, | |
), | |
), | |
); | |
final result = query.result; | |
final internalResult = result.parsedData?.getUser; | |
Widget child; | |
if (result.isLoading) { | |
child = const CircularProgressIndicator(); | |
} else if (result.hasException || internalResult == null) { | |
child = const Text('There was an issue loading your profile'); | |
} else { | |
child = _UserDisplay(internalResult.email, internalResult.displayName); | |
} | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Profile'), | |
), | |
body: Center( | |
child: child, | |
), | |
); | |
} | |
} | |
class _UserDisplay extends StatelessWidget { | |
final String _email; | |
final String? _displayName; | |
const _UserDisplay(this._email, this._displayName); | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: [ | |
if (_displayName != null) Text(_displayName!), | |
Text(_email), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment