:::

5-1 HTTP請求

  1. https://pub.dartlang.org/packages/http
  2. 開啟pubspec.yaml在dependencies下加入:
    dependencies:
      flutter:
        sdk: flutter
    
      # The following adds the Cupertino Icons font to your application.
      # Use with the CupertinoIcons class for iOS style icons.
      cupertino_icons: ^0.1.2
      url_launcher: ^5.0.1
      http: ^0.12.0+1

     

  3. 完整範例:
    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'http請求範例',
          home: new Scaffold(
            appBar: new AppBar(
              title: new Text('http請求範例'),
            ),
            body: new Center(
              child: new RaisedButton(
                onPressed: (){
                  var url='https://schoolweb.tn.edu.tw';
                  http.get(url).then((response){
                    print("狀態:${response.statusCode}");
                    print("正文:${response.body}");
                  });
                },
                child: new Text('發起http請求'),
              ),
            ),
          ),
        );
      }
    }
    

    執行結果可以從編輯器下方的「偵錯主控台」觀看(並不會呈現在主機上)


:::

書籍目錄

展開 | 闔起

快速登入


https%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1355%26tbsn%3D42

計數器

今天: 3423342334233423
昨天: 2489248924892489
總計: 8025405802540580254058025405802540580254058025405