Skip to content

Commit

Permalink
Prefer using Uri.parse instead of Uri.http/Uri.https (#5834)
Browse files Browse the repository at this point in the history
Using `Uri.http`/`Uri.https` are *much* more error-prone than using
`Uri.parse`, and judging from various StackOverflow questions, quite
a number of people either misunderstand how to use them or would have
avoided their problem by using `Uri.parse`.  Examples:

* https://stackoverflow.com/q/66619895/
* https://stackoverflow.com/a/66608366/
* https://stackoverflow.com/q/67535694/
* https://stackoverflow.com/q/67628555/
* https://stackoverflow.com/q/67451865/

IMO, we should advocate using `Uri.parse` when possible, particularly
in tutorials, where using `Uri.http`/`Uri.https` directly is a
premature optimization.
  • Loading branch information
jamesderlin authored May 21, 2021
1 parent 65722b7 commit 35d15fb
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:http/http.dart' as http;
// #docregion fetchAlbum
Future<Album> fetchAlbum() async {
final response =
await http.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1'));
await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));

if (response.statusCode == 200) {
// If the server did return a 200 OK response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import 'package:http/http.dart' as http;

// #docregion fetchAlbum
Future<http.Response> fetchAlbum() {
return http.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1'));
return http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
}
// #enddocregion fetchAlbum
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:http/http.dart' as http;
// #docregion createAlbum
Future<Album> createAlbum(String title) async {
final response = await http.post(
Uri.https('jsonplaceholder.typicode.com', 'albums'),
Uri.parse('https://jsonplaceholder.typicode.com/albums'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:http/http.dart' as http;
// #docregion fetchAlbum
Future<Album> fetchAlbum(http.Client client) async {
final response =
await client.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1'));
await client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));

if (response.statusCode == 200) {
// If the server did return a 200 OK response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {

// Use Mockito to return a successful response when it calls the
// provided http.Client.
when(client.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1')))
when(client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')))
.thenAnswer((_) async => http.Response('{"userId": 1, "id": 2, "title": "mock"}', 200));

expect(await fetchAlbum(client), isA<Album>());
Expand All @@ -29,7 +29,7 @@ void main() {

// Use Mockito to return an unsuccessful response when it calls the
// provided http.Client.
when(client.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1')))
when(client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')))
.thenAnswer((_) async => http.Response('Not Found', 404));

expect(fetchAlbum(client), throwsException);
Expand Down
7 changes: 3 additions & 4 deletions src/docs/cookbook/networking/fetch-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ This recipe covers how to fetch a sample album from the
<?code-excerpt "lib/main_step1.dart (fetchAlbum)"?>
```dart
Future<http.Response> fetchAlbum() {
return http.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1'));
return http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
}
```

Expand Down Expand Up @@ -134,7 +134,7 @@ function to return a `Future<Album>`:
```dart
Future<Album> fetchAlbum() async {
final response =
await http.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1'));
await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
Expand Down Expand Up @@ -252,7 +252,7 @@ import 'package:http/http.dart' as http;
Future<Album> fetchAlbum() async {
final response =
await http.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1'));
await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
Expand Down Expand Up @@ -349,4 +349,3 @@ class _MyAppState extends State<MyApp> {
[Mock dependencies using Mockito]: /docs/cookbook/testing/unit/mocking
[JSON and serialization]: /docs/development/data-and-backend/json
[`State`]: {{site.api}}/flutter/widgets/State-class.html

6 changes: 3 additions & 3 deletions src/docs/cookbook/networking/send-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ by sending an album title to the
```dart
Future<http.Response> createAlbum(String title) {
return http.post(
Uri.https('jsonplaceholder.typicode.com', 'albums'),
Uri.parse('https://jsonplaceholder.typicode.com/albums'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
Expand Down Expand Up @@ -135,7 +135,7 @@ function to return a `Future<Album>`:
```dart
Future<Album> createAlbum(String title) async {
final response = await http.post(
Uri.https('jsonplaceholder.typicode.com', 'albums'),
Uri.parse('https://jsonplaceholder.typicode.com/albums'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
Expand Down Expand Up @@ -244,7 +244,7 @@ import 'package:http/http.dart' as http;
Future<Album> createAlbum(String title) async {
final response = await http.post(
Uri.https('jsonplaceholder.typicode.com', 'albums'),
Uri.parse('https://jsonplaceholder.typicode.com/albums'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
Expand Down
12 changes: 6 additions & 6 deletions src/docs/cookbook/testing/unit/mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ The function should now look like this:
```dart
Future<Album> fetchAlbum(http.Client client) async {
final response =
await client.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1'));
await client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
Expand Down Expand Up @@ -174,7 +174,7 @@ void main() {
// Use Mockito to return a successful response when it calls the
// provided http.Client.
when(client.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1')))
when(client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')))
.thenAnswer((_) async => http.Response('{"userId": 1, "id": 2, "title": "mock"}', 200));
expect(await fetchAlbum(client), isA<Album>());
Expand All @@ -185,7 +185,7 @@ void main() {
// Use Mockito to return an unsuccessful response when it calls the
// provided http.Client.
when(client.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1')))
when(client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')))
.thenAnswer((_) async => http.Response('Not Found', 404));
expect(fetchAlbum(client), throwsException);
Expand Down Expand Up @@ -220,7 +220,7 @@ import 'package:http/http.dart' as http;
Future<Album> fetchAlbum(http.Client client) async {
final response =
await client.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1'));
await client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
Expand Down Expand Up @@ -321,7 +321,7 @@ void main() {
// Use Mockito to return a successful response when it calls the
// provided http.Client.
when(client.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1')))
when(client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')))
.thenAnswer((_) async => http.Response('{"userId": 1, "id": 2, "title": "mock"}', 200));
expect(await fetchAlbum(client), isA<Album>());
Expand All @@ -332,7 +332,7 @@ void main() {
// Use Mockito to return an unsuccessful response when it calls the
// provided http.Client.
when(client.get(Uri.https('jsonplaceholder.typicode.com', 'albums/1')))
when(client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')))
.thenAnswer((_) async => http.Response('Not Found', 404));
expect(fetchAlbum(client), throwsException);
Expand Down
2 changes: 1 addition & 1 deletion src/docs/get-started/flutter-for/react-native-devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,7 @@ GET, POST, PUT, and DELETE.
<!-- skip -->
```dart
// Flutter
final url = Uri.https('httpbin.org', 'ip');
final url = Uri.parse('https://httpbin.org/ip');
final httpClient = HttpClient();
_getIPAddress() async {
var request = await httpClient.getUrl(url);
Expand Down

0 comments on commit 35d15fb

Please sign in to comment.