diff --git a/CHANGELOG.md b/CHANGELOG.md index 27575ef..6ce80dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.6.0 - 16 May 2021 + * Fix for Flutter 2 Null safety version + * Lint apply + ## 0.5.2 - 30 Dec 2019 * Fix for Flutter 1.12.13+hotfix.5 diff --git a/README.md b/README.md index dd6ed7e..1211b19 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,44 @@ A plugin that connects Flutter to the Chrome Dev Tools on Android devices via the [Stetho Android Library](http://facebook.github.io/stetho/). +## Bug on Inspector of Google Chrome + +info: In the last update of Google Chrome, there is a bug in the inspect, so use other browsers based on the older versions of chrome, like [Brave](https://brave.com/download/). + +## Install +Add `flutter_stetho` as dependency to your `pubspec.yaml` + +```yaml +dev_dependencies: + flutter_stetho: + git: + url: git://github.com/irdevp/flutter_stetho.git + ref: master +``` + +Add on `main.dart` + +```dart +import 'package:flutter_stetho/flutter_stetho.dart'; + +void main() { + Stetho.initialize(); + + runApp(MyApp()); +} +``` + +## Tutorial Network Inspector on Brave + +navigate to `chrome://inspect` + +Network Inspector Brave + ## Network Inspector The main feature I was aiming to achieve was a Network Inspector. -Network Inspector in Action +Network Inspector in Action ## Getting Started @@ -19,6 +52,7 @@ Add `flutter_stetho` to your dependencies in the `pubspec.yaml` file - For Flutter 1.7.x, use version `0.3.x` - For Flutter 1.8.x, use version `0.4.x` - For Flutter 1.9.x, use version `0.5.x` + - For Flutter 2.x, use version `0.6.x` ### Install StethoHttpOverrides diff --git a/assets/example.gif b/assets/example.gif new file mode 100644 index 0000000..9d32c9f Binary files /dev/null and b/assets/example.gif differ diff --git a/example/lib/main.dart b/example/lib/main.dart index 9dbecd2..57c4f4d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -5,64 +5,66 @@ import 'package:http/http.dart' as http; void main() { Stetho.initialize(); - runApp(new FlutterStethoExample( - client: new http.Client(), + runApp(FlutterStethoExample( + client: http.Client(), )); } class FlutterStethoExample extends StatelessWidget { final http.Client client; - FlutterStethoExample({Key key, this.client}) : super(key: key); + const FlutterStethoExample({Key? key, required this.client}) + : super(key: key); - fetchImage() { + void Function()? fetchImage() { client.get( - 'https://flutter.dev/assets/404/dash_nest-c64796b59b65042a2b40fae5764c13b7477a592db79eaf04c86298dcb75b78ea.png', + Uri.parse( + 'https://flutter.dev/assets/404/dash_nest-c64796b59b65042a2b40fae5764c13b7477a592db79eaf04c86298dcb75b78ea.png'), headers: {'Authorization': 'token'}, ); } - fetchJson() { + void Function()? fetchJson() { client.get( - 'https://jsonplaceholder.typicode.com/posts/1', + Uri.parse('https://jsonplaceholder.typicode.com/posts/1'), headers: {'Authorization': 'token'}, ); } - fetchError() { - client.get('https://jsonplaceholder.typicode.com/postadsass/1'); + void Function()? fetchError() { + client.get(Uri.parse('https://jsonplaceholder.typicode.com/postadsass/1')); } @override Widget build(BuildContext context) { - return new MaterialApp( - home: new Scaffold( - appBar: new AppBar( - title: new Text('Plugin example app'), + return MaterialApp( + home: Scaffold( + appBar: AppBar( + title: const Text('Plugin example app'), ), - body: new Center( - child: new Column( + body: Center( + child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - new Padding( - padding: new EdgeInsets.all(16.0), - child: new RaisedButton( + Padding( + padding: const EdgeInsets.all(16.0), + child: ElevatedButton( onPressed: fetchJson, - child: new Text("Fetch json"), + child: const Text("Fetch json"), ), ), - new Padding( - padding: new EdgeInsets.all(16.0), - child: new RaisedButton( + Padding( + padding: const EdgeInsets.all(16.0), + child: ElevatedButton( onPressed: fetchImage, - child: new Text("Fetch image"), + child: const Text("Fetch image"), ), ), - new Padding( - padding: new EdgeInsets.all(16.0), - child: new RaisedButton( + Padding( + padding: const EdgeInsets.all(16.0), + child: ElevatedButton( onPressed: fetchError, - child: new Text("Fetch with Error"), + child: const Text("Fetch with Error"), ), ) ], diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 496dcef..fab9ca4 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,16 +1,23 @@ name: flutter_stetho_example -description: Demonstrates how to use the flutter_stetho plugin. +description: Demonstrates how to use the flutter_stetho plugin for flutter 2.0. + +environment: + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: sdk: flutter - http: ^0.12.0 + http: ^0.13.3 + dev_dependencies: - flutter_test: - sdk: flutter flutter_stetho: path: ../ + flutter_test: + sdk: flutter + + + flutter: - uses-material-design: true + uses-material-design: true \ No newline at end of file diff --git a/lib/src/http_client.dart b/lib/src/http_client.dart index 57339de..4cb6fb5 100644 --- a/lib/src/http_client.dart +++ b/lib/src/http_client.dart @@ -7,9 +7,12 @@ import 'package:flutter_stetho/src/method_channel_controller.dart'; import 'package:flutter_stetho/src/utils.dart'; class StethoHttpClient implements HttpClient { - final HttpClient client; + final HttpClient? client; - StethoHttpClient(this.client); + StethoHttpClient( + {required this.client, + this.autoUncompress = false, + this.idleTimeout = const Duration(seconds: 5)}); @override bool autoUncompress; @@ -18,13 +21,13 @@ class StethoHttpClient implements HttpClient { Duration idleTimeout; @override - Duration connectionTimeout; + Duration? connectionTimeout; @override - int maxConnectionsPerHost; + int? maxConnectionsPerHost; @override - String userAgent; + String? userAgent; @override void addCredentials( @@ -32,7 +35,7 @@ class StethoHttpClient implements HttpClient { String realm, HttpClientCredentials credentials, ) { - client.addCredentials(url, realm, credentials); + client!.addCredentials(url, realm, credentials); } @override @@ -42,33 +45,34 @@ class StethoHttpClient implements HttpClient { String realm, HttpClientCredentials credentials, ) { - client.addProxyCredentials(host, port, realm, credentials); + client!.addProxyCredentials(host, port, realm, credentials); } @override set authenticate( - Future Function(Uri url, String scheme, String realm) f, + Future Function(Uri url, String scheme, String realm)? f, ) { - client.authenticate = f; + client!.authenticate = f; } @override set authenticateProxy( - Future Function(String host, int port, String scheme, String realm) f, + Future Function(String host, int port, String scheme, String realm)? + f, ) { - client.authenticateProxy = f; + client!.authenticateProxy = f; } @override set badCertificateCallback( - bool Function(X509Certificate cert, String host, int port) callback, + bool Function(X509Certificate cert, String host, int port)? callback, ) { - client.badCertificateCallback = callback; + client!.badCertificateCallback = callback; } @override - void close({bool force: false}) { - client.close(); + void close({bool force = false}) { + client!.close(); } @override @@ -114,7 +118,7 @@ class StethoHttpClient implements HttpClient { Future patchUrl(Uri url) => openUrl("patch", url); @override - set findProxy(String Function(Uri url) f) => client.findProxy = f; + set findProxy(String Function(Uri url)? f) => client!.findProxy = f; @override Future open( @@ -123,19 +127,19 @@ class StethoHttpClient implements HttpClient { int port, String path, ) async { - Uri uri = Uri(host: host,port: port, path: path); - return await openUrl(method, uri); + final Uri uri = Uri(host: host, port: port, path: path); + return openUrl(method, uri); } @override Future openUrl(String method, Uri url) async { - return client.openUrl(method, url).then((request) { + return client!.openUrl(method, url).then((request) { final wrapped = _wrapResponse(request); - List body = []; - if (method.toLowerCase() != 'post' && method.toLowerCase() != 'put'){ + final List body = []; + if (method.toLowerCase() != 'post' && method.toLowerCase() != 'put') { scheduleMicrotask(() { MethodChannelController.requestWillBeSent( - new FlutterStethoInspectorRequest( + FlutterStethoInspectorRequest( url: request.uri.toString(), headers: headersToMap(request.headers), method: request.method, @@ -146,10 +150,10 @@ class StethoHttpClient implements HttpClient { }); } else { wrapped.stream.listen((onData) { - body.addAll(onData); + body.addAll(onData as Iterable); scheduleMicrotask(() { MethodChannelController.requestWillBeSent( - new FlutterStethoInspectorRequest( + FlutterStethoInspectorRequest( url: request.uri.toString(), headers: headersToMap(request.headers), method: request.method, @@ -166,8 +170,8 @@ class StethoHttpClient implements HttpClient { } StethoHttpClientRequest _wrapResponse(HttpClientRequest request) { - final id = new Uuid().generateV4(); + final id = Uuid().generateV4(); - return new StethoHttpClientRequest(request, id); + return StethoHttpClientRequest(request, id); } } diff --git a/lib/src/http_client_request.dart b/lib/src/http_client_request.dart index e7f9c14..43983e4 100644 --- a/lib/src/http_client_request.dart +++ b/lib/src/http_client_request.dart @@ -10,7 +10,8 @@ import 'package:flutter_stetho/src/utils.dart'; class StethoHttpClientRequest implements HttpClientRequest { final HttpClientRequest request; final String id; - final StreamController> _streamController = StreamController.broadcast(); + final StreamController> _streamController = + StreamController.broadcast(); Stream get stream => _streamController.stream.asBroadcastStream(); StethoHttpClientRequest( @@ -25,14 +26,14 @@ class StethoHttpClientRequest implements HttpClientRequest { } @override - void addError(Object error, [StackTrace stackTrace]) { + void addError(Object error, [StackTrace? stackTrace]) { request.addError(error, stackTrace); } @override Future addStream(Stream> stream) { - var newStream = stream.asBroadcastStream(); - newStream.listen((onData)=> _streamController.add(onData)); + final newStream = stream.asBroadcastStream(); + newStream.listen((onData) => _streamController.add(onData)); return request.addStream(newStream); } @@ -40,7 +41,7 @@ class StethoHttpClientRequest implements HttpClientRequest { Future close() async { final response = await request.close(); MethodChannelController.responseHeadersReceived( - new FlutterStethoInspectorResponse( + FlutterStethoInspectorResponse( url: request.uri.toString(), statusCode: response.statusCode, requestId: id, @@ -53,7 +54,7 @@ class StethoHttpClientRequest implements HttpClientRequest { MethodChannelController.interpretResponseStream(id); - return new StethoHttpClientResponse( + return StethoHttpClientResponse( response, response.transform(createResponseTransformer(id)), ); @@ -98,7 +99,7 @@ class StethoHttpClientRequest implements HttpClientRequest { request.persistentConnection = persistentConnection; @override - HttpConnectionInfo get connectionInfo => request.connectionInfo; + HttpConnectionInfo? get connectionInfo => request.connectionInfo; @override List get cookies => request.cookies; @@ -119,14 +120,15 @@ class StethoHttpClientRequest implements HttpClientRequest { Uri get uri => request.uri; @override - void write(Object obj) { + void write(Object? obj) { request.write(obj); } @override void writeAll(Iterable objects, [String separator = ""]) { request.writeAll(objects, separator); - String data = objects.map((object) => object.toString()).join(separator); + final String data = + objects.map((object) => object.toString()).join(separator); _streamController.add(data.codeUnits); } @@ -137,12 +139,15 @@ class StethoHttpClientRequest implements HttpClientRequest { } @override - void writeln([Object obj = ""]) { + void writeln([Object? obj = ""]) { request.writeln(obj); - if (obj is String){ + if (obj is String) { _streamController.add(obj.codeUnits); } else { _streamController.add(obj.toString().codeUnits); } } + + @override + void abort([Object? exception, StackTrace? stackTrace]) {} } diff --git a/lib/src/http_client_response.dart b/lib/src/http_client_response.dart index 0d62b5c..9c30269 100644 --- a/lib/src/http_client_response.dart +++ b/lib/src/http_client_response.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'dart:io'; -import 'dart:typed_data'; class StethoHttpClientResponse extends StreamView> implements HttpClientResponse { @@ -10,10 +9,10 @@ class StethoHttpClientResponse extends StreamView> : super(stream); @override - X509Certificate get certificate => response.certificate; + X509Certificate? get certificate => response.certificate; @override - HttpConnectionInfo get connectionInfo => response.connectionInfo; + HttpConnectionInfo? get connectionInfo => response.connectionInfo; @override int get contentLength => response.contentLength; @@ -40,7 +39,7 @@ class StethoHttpClientResponse extends StreamView> @override Future redirect( - [String method, Uri url, bool followLoops]) { + [String? method, Uri? url, bool? followLoops]) { return response.redirect(method, url, followLoops); } diff --git a/lib/src/http_overrides.dart b/lib/src/http_overrides.dart index 29e2c41..dac2d3b 100644 --- a/lib/src/http_overrides.dart +++ b/lib/src/http_overrides.dart @@ -3,9 +3,9 @@ import 'dart:io'; import 'package:flutter_stetho/src/http_client.dart'; class StethoHttpOverrides extends HttpOverrides { - final String Function(Uri url, Map environment) + final String Function(Uri url, Map environment)? findProxyFromEnvironmentFn; - final HttpClient Function(SecurityContext context) createHttpClientFn; + final HttpClient Function(SecurityContext context)? createHttpClientFn; StethoHttpOverrides({ this.findProxyFromEnvironmentFn, @@ -13,22 +13,22 @@ class StethoHttpOverrides extends HttpOverrides { }); @override - HttpClient createHttpClient(SecurityContext context) { + HttpClient createHttpClient(SecurityContext? context) { final client = createHttpClientFn != null - ? createHttpClientFn(context) + ? createHttpClientFn!(context!) : super.createHttpClient(context); if (Platform.isAndroid) { - return new StethoHttpClient(client); + return StethoHttpClient(client: client); } return client; } @override - String findProxyFromEnvironment(Uri url, Map environment) { + String findProxyFromEnvironment(Uri? url, Map? environment) { return findProxyFromEnvironmentFn != null - ? findProxyFromEnvironmentFn(url, environment) - : super.findProxyFromEnvironment(url, environment); + ? findProxyFromEnvironmentFn!(url!, environment!) + : super.findProxyFromEnvironment(url!, environment); } } diff --git a/lib/src/inspector_request.dart b/lib/src/inspector_request.dart index 86d8cb7..6a5b79c 100644 --- a/lib/src/inspector_request.dart +++ b/lib/src/inspector_request.dart @@ -1,19 +1,17 @@ -import 'package:flutter/foundation.dart'; - class FlutterStethoInspectorRequest { - final int friendlyNameExtra; + final int? friendlyNameExtra; final String url; final String method; - final List body; + final List? body; final String id; final String friendlyName; final Map headers; FlutterStethoInspectorRequest({ - @required this.url, - @required this.method, - @required this.id, - @required this.headers, + required this.url, + required this.method, + required this.id, + required this.headers, this.body, this.friendlyName = 'Flutter Stetho', this.friendlyNameExtra, diff --git a/lib/src/inspector_response.dart b/lib/src/inspector_response.dart index 4a09dc4..af85172 100644 --- a/lib/src/inspector_response.dart +++ b/lib/src/inspector_response.dart @@ -1,5 +1,3 @@ -import 'package:flutter/foundation.dart'; - class FlutterStethoInspectorResponse { final String url; final bool connectionReused; @@ -11,13 +9,13 @@ class FlutterStethoInspectorResponse { final Map headers; FlutterStethoInspectorResponse({ - @required this.url, - @required this.connectionReused, - @required this.connectionId, - @required this.requestId, - @required this.statusCode, - @required this.reasonPhrase, - @required this.headers, + required this.url, + required this.connectionReused, + required this.connectionId, + required this.requestId, + required this.statusCode, + required this.reasonPhrase, + required this.headers, this.fromDiskCache = false, }); diff --git a/lib/src/method_channel_controller.dart b/lib/src/method_channel_controller.dart index abea086..86dc210 100644 --- a/lib/src/method_channel_controller.dart +++ b/lib/src/method_channel_controller.dart @@ -5,7 +5,7 @@ import 'package:flutter_stetho/src/inspector_request.dart'; import 'package:flutter_stetho/src/inspector_response.dart'; class MethodChannelController { - static const MethodChannel _channel = const MethodChannel('flutter_stetho'); + static const MethodChannel _channel = MethodChannel('flutter_stetho'); static Future requestWillBeSent( FlutterStethoInspectorRequest request) => diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 9a40a08..51774ba 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -1,14 +1,13 @@ import 'dart:async'; import 'dart:io'; import 'dart:math'; -import 'dart:typed_data'; import 'package:flutter_stetho/src/method_channel_controller.dart'; /// Create a response transformer that can intercept and pipe the http response /// data to the Method channel StreamTransformer, List> createResponseTransformer(String id) { - return new StreamTransformer.fromHandlers(handleData: (data, sink) { + return StreamTransformer.fromHandlers(handleData: (data, sink) { sink.add(data); MethodChannelController.onDataReceived({"data": data, "id": id}); }, handleError: (error, stacktrace, sink) { @@ -46,7 +45,7 @@ Map headersToMap(HttpHeaders headers) { /// /// final String id = new Uuid().generateV4(); class Uuid { - final Random _random = new Random(); + final Random _random = Random(); /// Generate a version 4 (random) uuid. This is a uuid scheme that only uses /// random numbers as the source of the generated uuid. diff --git a/pubspec.yaml b/pubspec.yaml index a5733be..407e0a6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,16 +1,15 @@ name: flutter_stetho description: A network inspector for Flutter on Android using the Chrome Dev Tools -version: 0.5.2 +version: 0.6.0 author: Brian Egan homepage: https://github.com/brianegan/flutter_stetho environment: - sdk: '>=2.0.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: sdk: flutter - flutter: plugin: androidPackage: com.brianegan.flutterstetho