Skip to content

Commit 3709d8f

Browse files
committed
Revised and updated example
1 parent 991b1d5 commit 3709d8f

13 files changed

+11912
-51
lines changed

04_http/.vscode/launch.json

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"packages/cupertino_icons/assets/CupertinoIcons.ttf":["packages/cupertino_icons/assets/CupertinoIcons.ttf"]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"fonts":[{"asset":"fonts/MaterialIcons-Regular.ttf"}],"family":"MaterialIcons"},{"family":"packages/cupertino_icons/CupertinoIcons","fonts":[{"asset":"packages/cupertino_icons/assets/CupertinoIcons.ttf"}]}]

04_http/ios/Flutter/flutter_assets/LICENSE

+11,864
Large diffs are not rendered by default.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5.87 MB
Binary file not shown.
Binary file not shown.

04_http/ios/Runner.xcodeproj/project.pbxproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
/* Begin PBXBuildFile section */
1010
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
1111
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
12+
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */ = {isa = PBXBuildFile; fileRef = 2D5378251FAA1A9400D5DBA9 /* flutter_assets */; };
1213
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
1314
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1415
9705A1C61CF904A100538489 /* Flutter.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9740EEBA1CF902C7004384FC /* Flutter.framework */; };
1516
9705A1C71CF904A300538489 /* Flutter.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9740EEBA1CF902C7004384FC /* Flutter.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1617
9740EEB41CF90195004384FC /* Debug.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB21CF90195004384FC /* Debug.xcconfig */; };
1718
9740EEB51CF90195004384FC /* Generated.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB31CF90195004384FC /* Generated.xcconfig */; };
18-
9740EEBB1CF902C7004384FC /* app.flx in Resources */ = {isa = PBXBuildFile; fileRef = 9740EEB71CF902C7004384FC /* app.flx */; };
1919
978B8F6F1D3862AE00F588F7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */; };
2020
97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; };
2121
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
@@ -42,13 +42,13 @@
4242
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
4343
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
4444
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
45+
2D5378251FAA1A9400D5DBA9 /* flutter_assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = flutter_assets; path = Flutter/flutter_assets; sourceTree = SOURCE_ROOT; };
4546
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
4647
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
4748
7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
4849
7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
4950
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = "<group>"; };
5051
9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = "<group>"; };
51-
9740EEB71CF902C7004384FC /* app.flx */ = {isa = PBXFileReference; lastKnownFileType = file; name = app.flx; path = Flutter/app.flx; sourceTree = "<group>"; };
5252
9740EEBA1CF902C7004384FC /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = Flutter/Flutter.framework; sourceTree = "<group>"; };
5353
97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; };
5454
97C146F21CF9000F007C117D /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -74,9 +74,9 @@
7474
9740EEB11CF90186004384FC /* Flutter */ = {
7575
isa = PBXGroup;
7676
children = (
77-
9740EEB71CF902C7004384FC /* app.flx */,
7877
3B80C3931E831B6300D905FE /* App.framework */,
7978
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */,
79+
2D5378251FAA1A9400D5DBA9 /* flutter_assets */,
8080
9740EEBA1CF902C7004384FC /* Flutter.framework */,
8181
9740EEB21CF90195004384FC /* Debug.xcconfig */,
8282
7AFA3C8E1D35360C0083082E /* Release.xcconfig */,
@@ -187,10 +187,10 @@
187187
isa = PBXResourcesBuildPhase;
188188
buildActionMask = 2147483647;
189189
files = (
190-
9740EEBB1CF902C7004384FC /* app.flx in Resources */,
191190
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */,
192191
9740EEB51CF90195004384FC /* Generated.xcconfig in Resources */,
193192
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
193+
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */,
194194
9740EEB41CF90195004384FC /* Debug.xcconfig in Resources */,
195195
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
196196
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */,

04_http/lib/main.dart

+39-32
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,74 @@
1-
// Copyright 2017 The Chromium Authors. All rights reserved.
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:flutter/widgets.dart';
5+
import 'dart:convert';
6+
7+
import 'package:flutter/material.dart';
68
import 'package:http/http.dart' as http;
79

810
void main() {
9-
runApp(new MyApp());
11+
runApp(NetworkApp());
1012
}
1113

12-
class MyApp extends StatelessWidget {
14+
class NetworkApp extends StatelessWidget {
1315
@override
1416
Widget build(BuildContext context) {
15-
return new Center(
16-
child: new Padding(padding: new EdgeInsets.only(top: 50.0),
17-
child: new Column(children: <Widget>[
18-
new FetchTextWidget(url: 'https://api.stackexchange.com/2.2/search?intitle=flutter&site=stackoverflow'),
19-
new Padding(padding: new EdgeInsets.only(top: 50.0)),
20-
new FetchImageWidget(url: 'https://flutter.io/images/flutter-mark-square-100.png'),
21-
])
22-
)
17+
return MaterialApp(
18+
home: Scaffold(body: NetworkBody()),
19+
);
20+
}
21+
}
22+
23+
class NetworkBody extends StatelessWidget {
24+
@override
25+
Widget build(BuildContext context) {
26+
return Center(
27+
child: Column(
28+
mainAxisAlignment: MainAxisAlignment.center,
29+
children: [
30+
Quotation(url: 'https://quotes.rest/qod.json'),
31+
Padding(padding: EdgeInsets.only(top: 50.0)),
32+
Image
33+
.network('https://flutter.io/images/flutter-mark-square-100.png'),
34+
],
35+
),
2336
);
2437
}
2538
}
2639

2740
/// The first line of a HTTP request body
28-
class FetchTextWidget extends StatefulWidget {
29-
FetchTextWidget({Key key, this.url}) : super(key: key);
41+
class Quotation extends StatefulWidget {
42+
Quotation({Key key, this.url}) : super(key: key);
3043

3144
final String url;
3245

3346
@override
34-
_FetchTextState createState() => new _FetchTextState();
47+
createState() => _QuotationState();
3548
}
3649

37-
class _FetchTextState extends State<FetchTextWidget> {
50+
class _QuotationState extends State<Quotation> {
3851
String data = 'Loading ...';
3952

40-
_get() async {
41-
var res = await http.get(widget.url);
42-
setState(() => data = res.body);
43-
}
44-
4553
@override
4654
void initState() {
4755
super.initState();
4856
_get();
4957
}
5058

51-
@override
52-
Widget build(BuildContext context) {
53-
return new Text(data, textDirection: TextDirection.ltr, overflow: TextOverflow.ellipsis);
59+
_get() async {
60+
final res = await http.get(widget.url);
61+
setState(() => data = _parseQuoteFromJson(res.body));
5462
}
55-
}
5663

57-
/// Fetch an image over the network and add to a widget
58-
class FetchImageWidget extends StatelessWidget {
59-
FetchImageWidget({this.url});
60-
61-
final String url;
64+
String _parseQuoteFromJson(String jsonStr) {
65+
// In the real world, this should check for errors
66+
final jsonQuote = json.decode(jsonStr);
67+
return jsonQuote['contents']['quotes'][0]['quote'];
68+
}
6269

63-
@override
70+
@override
6471
Widget build(BuildContext context) {
65-
return new Image.network(url);
72+
return Text(data, textAlign: TextAlign.center);
6673
}
6774
}

04_http/pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ dependencies:
55
flutter:
66
sdk: flutter
77

8+
http: ^0.11.3
9+
810
# The following adds the Cupertino Icons font to your application.
911
# Use with the CupertinoIcons class for iOS style icons.
1012
cupertino_icons: ^0.1.0

04_http/test/widget_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import '../lib/main.dart';
1111
void main() {
1212
testWidgets('HTTP loads', (WidgetTester tester) async {
1313
// Build our app and trigger a frame.
14-
await tester.pumpWidget(new MyApp());
14+
await tester.pumpWidget(NetworkApp());
1515
expect(find.text('Loading ...'), findsOneWidget);
1616
await tester.pumpAndSettle();
1717
});

0 commit comments

Comments
 (0)