From a08abfcaa9937834646ad9d4db19c388692a1ea9 Mon Sep 17 00:00:00 2001 From: Cal Pratt Date: Sun, 22 Jul 2018 15:34:08 -0700 Subject: [PATCH] Remove the use of new throughout the project --- README.md | 38 ++++++++++++------------- example/lib/main.dart | 40 +++++++++++++-------------- lib/src/cache/in_memory.dart | 10 +++---- lib/src/client.dart | 8 +++--- lib/src/widgets/cache_provider.dart | 2 +- lib/src/widgets/graphql_provider.dart | 4 +-- lib/src/widgets/mutation.dart | 2 +- lib/src/widgets/query.dart | 8 +++--- 8 files changed, 56 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index d40eaabd2..b1c5d1ad0 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,10 @@ To use the client it first needs to be initialized with an endpoint and cache. I import 'package:graphql_flutter/graphql_flutter.dart'; void main() { - ValueNotifier client = new ValueNotifier( - new Client( + ValueNotifier client = ValueNotifier( + Client( endPoint: 'https://api.github.com/graphql', - cache: new InMemoryCache(), + cache: InMemoryCache(), apiToken: '', ), ); @@ -69,9 +69,9 @@ In order to use the client, you app needs to be wrapped with the `GraphqlProvide ```dart ... - return new GraphqlProvider( + return GraphqlProvider( client: client, - child: new MaterialApp( + child: MaterialApp( title: 'Flutter Demo', ... ), @@ -106,7 +106,7 @@ In your widget: ```dart ... -new Query( +Query( readRepositories, // this is the query you just created variables: { 'nRepositories': 50, @@ -118,22 +118,22 @@ new Query( String error, }) { if (error != '') { - return new Text(error); + return Text(error); } if (loading) { - return new Text('Loading'); + return Text('Loading'); } // it can be either Map or List List repositories = data['viewer']['repositories']['nodes']; - return new ListView.builder( + return ListView.builder( itemCount: repositories.length, itemBuilder: (context, index) { final repository = repositories[index]; - return new Text(repository['name']); + return Text(repository['name']); }); }, ); @@ -163,7 +163,7 @@ The syntax for mutations is fairly similar to that of a query. The only diffence ```dart ... -new Mutation( +Mutation( addStar, builder: ( runMutation, { // you can name it whatever you like @@ -171,12 +171,12 @@ new Mutation( var data, String error, }) { - return new FloatingActionButton( + return FloatingActionButton( onPressed: () => runMutation({ 'starrableId': , }), tooltip: 'Star', - child: new Icon(Icons.star), + child: Icon(Icons.star), ); }, onCompleted: (Map data) { @@ -208,12 +208,12 @@ You can always access the client direcly from the `GraphqlProvider` but to make ```dart ... - return new GraphqlConsumer( + return GraphqlConsumer( builder: (Client client) { // do something with the client - return new Container( - child: new Text('Hello world'), + return Container( + child: Text('Hello world'), ); }, ); @@ -233,10 +233,10 @@ The in-memory cache can automatically be saved to and restored from offline stor class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return new GraphqlProvider( + return GraphqlProvider( client: client, - child: new CacheProvider( - child: new MaterialApp( + child: CacheProvider( + child: MaterialApp( title: 'Flutter Demo', ... ), diff --git a/example/lib/main.dart b/example/lib/main.dart index eb0d90986..f7412f1d8 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -4,28 +4,28 @@ import 'package:graphql_flutter/graphql_flutter.dart'; import './queries/readRepositories.dart' as queries; import './mutations/addStar.dart' as mutations; -void main() => runApp(new MyApp()); +void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - ValueNotifier client = new ValueNotifier( - new Client( + ValueNotifier client = ValueNotifier( + Client( endPoint: 'https://api.github.com/graphql', - cache: new InMemoryCache(), + cache: InMemoryCache(), apiToken: '', ), ); - return new GraphqlProvider( + return GraphqlProvider( client: client, - child: new CacheProvider( - child: new MaterialApp( + child: CacheProvider( + child: MaterialApp( title: 'Flutter Demo', - theme: new ThemeData( + theme: ThemeData( primarySwatch: Colors.blue, ), - home: new MyHomePage(title: 'Flutter Demo Home Page'), + home: MyHomePage(title: 'Flutter Demo Home Page'), ), ), ); @@ -41,17 +41,17 @@ class MyHomePage extends StatefulWidget { final String title; @override - _MyHomePageState createState() => new _MyHomePageState(); + _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override Widget build(BuildContext context) { - return new Scaffold( - appBar: new AppBar( - title: new Text(widget.title), + return Scaffold( + appBar: AppBar( + title: Text(widget.title), ), - body: new Query( + body: Query( queries.readRepositories, pollInterval: 1, builder: ({ @@ -60,22 +60,22 @@ class _MyHomePageState extends State { String error, }) { if (error != '') { - return new Text(error); + return Text(error); } if (loading) { - return new Text('Loading'); + return Text('Loading'); } // it can be either Map or List List repositories = data['viewer']['repositories']['nodes']; - return new ListView.builder( + return ListView.builder( itemCount: repositories.length, itemBuilder: (context, index) { final repository = repositories[index]; - return new Mutation( + return Mutation( mutations.addStar, builder: ( addStar, { @@ -88,11 +88,11 @@ class _MyHomePageState extends State { data['addStar']['starrable']['viewerHasStarred']; } - return new ListTile( + return ListTile( leading: repository['viewerHasStarred'] ? const Icon(Icons.star, color: Colors.amber) : const Icon(Icons.star_border), - title: new Text(repository['name']), + title: Text(repository['name']), // NOTE: optimistic ui updates are not implemented yet, therefore changes may take upto 1 second to show. onTap: () { addStar({ diff --git a/lib/src/cache/in_memory.dart b/lib/src/cache/in_memory.dart index dce71c710..3eb1f1e55 100644 --- a/lib/src/cache/in_memory.dart +++ b/lib/src/cache/in_memory.dart @@ -6,7 +6,7 @@ import 'dart:io'; import 'package:path_provider/path_provider.dart'; class InMemoryCache { - HashMap _inMemoryCache = new HashMap(); + HashMap _inMemoryCache = HashMap(); Future get _localStoragePath async { final Directory directory = await getApplicationDocumentsDirectory(); @@ -37,7 +37,7 @@ class InMemoryCache { try { final File file = await _localStorageFile; final HashMap storedHashMap = - new HashMap(); + HashMap(); if (file.existsSync()) { Stream inputStream = file.openRead(); @@ -45,7 +45,7 @@ class InMemoryCache { inputStream .transform(utf8.decoder) // Decode bytes to UTF8. .transform( - new LineSplitter()) // Convert stream to individual lines. + LineSplitter()) // Convert stream to individual lines. .listen((String line) { final List keyAndValue = json.decode(line); @@ -57,12 +57,12 @@ class InMemoryCache { } on FileSystemException { // TODO: handle No such file - return new HashMap(); + return HashMap(); } catch (error) { // TODO: handle error print(error); - return new HashMap(); + return HashMap(); } } diff --git a/lib/src/client.dart b/lib/src/client.dart index 9bdbee366..7210f2fa6 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -17,7 +17,7 @@ class Client { this.endPoint = endPoint; this.cache = cache; this.apiToken = apiToken; - this.client = new http.Client(); + this.client = http.Client(); } String _endpoint; @@ -67,7 +67,7 @@ class Client { final String reasonPhrase = response.reasonPhrase; if (statusCode < 200 || statusCode >= 400) { - throw new http.ClientException( + throw http.ClientException( 'Network Error: $statusCode $reasonPhrase', ); } @@ -75,7 +75,7 @@ class Client { final Map jsonResponse = json.decode(response.body); if (jsonResponse['errors'] != null && jsonResponse['errors'].length > 0) { - throw new Exception( + throw Exception( 'Error returned by the server in the query' + jsonResponse['errors'].toString(), ); @@ -124,7 +124,7 @@ class Client { if (cache.hasEntity(body)) { return cache.read(body); } else { - throw new Exception('Can\'t find field in cache.'); + throw Exception('Can\'t find field in cache.'); } } } diff --git a/lib/src/widgets/cache_provider.dart b/lib/src/widgets/cache_provider.dart index ea866d651..a2a9d0ce7 100644 --- a/lib/src/widgets/cache_provider.dart +++ b/lib/src/widgets/cache_provider.dart @@ -12,7 +12,7 @@ class CacheProvider extends StatefulWidget { final Widget child; @override - _CacheProviderState createState() => new _CacheProviderState(); + _CacheProviderState createState() => _CacheProviderState(); } class _CacheProviderState extends State diff --git a/lib/src/widgets/graphql_provider.dart b/lib/src/widgets/graphql_provider.dart index 04ea55ce3..e352437b2 100644 --- a/lib/src/widgets/graphql_provider.dart +++ b/lib/src/widgets/graphql_provider.dart @@ -20,7 +20,7 @@ class GraphqlProvider extends StatefulWidget { } @override - State createState() => new _GraphqlProviderState(); + State createState() => _GraphqlProviderState(); } class _GraphqlProviderState extends State { @@ -42,7 +42,7 @@ class _GraphqlProviderState extends State { @override Widget build(BuildContext context) { - return new _InheritedGraphqlProvider( + return _InheritedGraphqlProvider( client: widget.client, child: widget.child, ); diff --git a/lib/src/widgets/mutation.dart b/lib/src/widgets/mutation.dart index 9c35249fb..6a22e6064 100644 --- a/lib/src/widgets/mutation.dart +++ b/lib/src/widgets/mutation.dart @@ -25,7 +25,7 @@ class Mutation extends StatefulWidget { final OnMutationCompleted onCompleted; @override - MutationState createState() => new MutationState(); + MutationState createState() => MutationState(); } class MutationState extends State { diff --git a/lib/src/widgets/query.dart b/lib/src/widgets/query.dart index fd31bff53..60105ce00 100644 --- a/lib/src/widgets/query.dart +++ b/lib/src/widgets/query.dart @@ -25,7 +25,7 @@ class Query extends StatefulWidget { final int pollInterval; @override - QueryState createState() => new QueryState(); + QueryState createState() => QueryState(); } class QueryState extends State { @@ -36,14 +36,14 @@ class QueryState extends State { bool initialFetch = true; Duration pollInterval; Timer pollTimer; - Map currentVariables = new Map(); + Map currentVariables = Map(); @override void initState() { super.initState(); if (widget.pollInterval is int) { - pollInterval = new Duration(seconds: widget.pollInterval); + pollInterval = Duration(seconds: widget.pollInterval); } getQueryResult(); @@ -107,7 +107,7 @@ class QueryState extends State { } if (pollInterval is Duration && !(pollTimer is Timer)) { - pollTimer = new Timer.periodic( + pollTimer = Timer.periodic( pollInterval, (Timer t) => getQueryResult(), );