cupertino_icons: ^0.1.2
    http: ^0.12.0+1
    dio: ^1.0.13
    html: ^0.13.3+3
    json_annotation: ^2.0.0

    1. import 'dart:convert';
    2. import 'package:flutter/material.dart';
    3. import 'package:http/http.dart' as http;
    4. import 'package:dio/dio.dart';
    5. import 'dart:async';
    6. import 'data/class1.dart';
    7. Dio dio = Dio();
    8. Future<Post1> fetchPost() async {
    9. Response response = await dio.get('https://jsonplaceholder.typicode.com/posts/1');
    10. print(response.data.runtimeType);
    11. return new Post1.fromJson(response.data);
    12. }
    13. // 解析json
    14. class Post1 {
    15. final int userId;
    16. final int id;
    17. final String title;
    18. final String body;
    19. Post1({this.userId, this.id, this.title, this.body});
    20. factory Post1.fromJson(Map<String, dynamic> json) {
    21. return new Post1(
    22. userId: json['userId'],
    23. id: json['id'],
    24. title: json['title'],
    25. body: json['body'],
    26. );
    27. }
    28. }
    29. void main() => runApp(new MyApp());
    30. class MyApp extends StatelessWidget {
    31. @override
    32. Widget build(BuildContext context) {
    33. return new MaterialApp(
    34. title: 'Fetch Data Example',
    35. theme: new ThemeData(
    36. primarySwatch: Colors.red,
    37. ),
    38. home: new Scaffold(
    39. appBar: new AppBar(
    40. title: new Text('Fetch Data Example'),
    41. ),
    42. body: new Center(
    43. child: new FutureBuilder<Post1>(
    44. future: fetchPost(),
    45. builder: (context, snapshot) {
    46. if (snapshot.hasData) {
    47. return new Text(snapshot.data.title);
    48. } else if (snapshot.hasError) {
    49. return new Text("${snapshot.error}");
    50. }
    51. // By default, show a loading spinner
    52. return new CircularProgressIndicator();
    53. },
    54. ),
    55. ),
    56. ),
    57. );
    58. }
    59. }