美文网首页
Flutter 4.1 网络请求

Flutter 4.1 网络请求

作者: 贝灬小晖 | 来源:发表于2020-03-13 16:33 被阅读0次

    1.json字符串转map

    json.decode(strData)

    2.map转json字符串

    json.encode(mapData)

    需要引入 dart:convert 这个库

    http 第三方库 自己看就好了

    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    import 'dart:convert' as convert;
    
    class MyRequest extends StatelessWidget {
      _getRequest() async{
    var url = "http://route.showapi.com/1211-1";
    var response = await http.get(url);
      if (response.statusCode == 200) {
        var jsonResponse = convert.jsonDecode(response.body);
        var itemCount = jsonResponse['totalItems'];
        print('Number of books about http: $jsonResponse.');
      } else {
        print('Request failed with status: ${response.statusCode}.');
      }
    
      }
    
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("请求"),
          ),
          body: Column(
            children: <Widget>[
             RaisedButton(
               color: Colors.red,
               
               onPressed: _getRequest
             ),
             RaisedButton.icon(
               onPressed: (){
    
               },
               label: Text("data"),
               icon: Icon(Icons.save),
             )
            ],
    
    
    
          ),
        );
      }
    }
    

    相关文章

      网友评论

          本文标题:Flutter 4.1 网络请求

          本文链接:https://www.haomeiwen.com/subject/qlorshtx.html