zoukankan      html  css  js  c++  java
  • flutter http get请求

    import 'dart:async';
    import 'dart:convert';
    
    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    
    Future<Post> fetchPost() async {
      final response =
      await http.get('https://jsonplaceholder.typicode.com/posts/1');
      final responseJson = json.decode(response.body);
    
      return new Post.fromJson(responseJson);
    }
    
    class Post {
      final int userId;
      final int id;
      final String title;
      final String body;
    
      Post({this.userId, this.id, this.title, this.body});
    
      factory Post.fromJson(Map<String, dynamic> json) {
        return new Post(
          userId: json['userId'],
          id: json['id'],
          title: json['title'],
          body: json['body'],
        );
      }
    }
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Fetch Data Example',
          theme: new ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: new Scaffold(
            appBar: new AppBar(
              title: new Text('Fetch Data Example'),
            ),
            body: new Center(
              child: new FutureBuilder<Post>(
                future: fetchPost(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return new Text(snapshot.data.title);
                  } else if (snapshot.hasError) {
                    return new Text("${snapshot.error}");
                  }
    
                  // By default, show a loading spinner
                  return new CircularProgressIndicator();
                },
              ),
            ),
          ),
        );
      }
    }

  • 相关阅读:
    文件下载断点续传插件webupload插件
    cocos2dx 2.x 粒子渲染时有黑色粒BUG
    VOIP NAT穿越之SIP信令穿越
    hdu 5086 Revenge of Segment Tree(BestCoder Round #16)
    [并发]线程池技术小白
    调用 COM 对象
    switch-case 执行顺序
    HDELETE
    python and java
    部分查询练习题及答案
  • 原文地址:https://www.cnblogs.com/sea-stream/p/12158967.html
Copyright © 2011-2022 走看看