Flutter异步编程 http网络请求数据

Posted 码上加油站

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter异步编程 http网络请求数据相关的知识,希望对你有一定的参考价值。

import \'dart:convert\';

import \'package:flutter/material.dart\';
import \'package:http/http.dart\' as http;
import \'dart:async\';

class HttpDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(\'HttpDemo\'),
        elevation: 0.0,
      ),
      body: HttpDemoHome(),
    );
  }
}

class HttpDemoHome extends StatefulWidget {
  @override
  _HttpDemoHomeState createState() => _HttpDemoHomeState();
}

class _HttpDemoHomeState extends State<HttpDemoHome> {
  @override
  void initState() {
    super.initState();
    // fetchPosts()
    //   .then((value) => print(value));

    // final post = {
    //   \'title\': \'hello\',
    //   \'description\': \'nice to meet you.\',
    // };

    // print(post[\'title\']);
    // print(post[\'description\']);

    // final postJson = json.encode(post);
    // print(postJson);

    // final postJsonConverted = json.decode(postJson);
    // print(postJsonConverted[\'title\']);
    // print(postJsonConverted[\'description\']);
    // print(postJsonConverted is Map);

    // final postModel = Post.fromJson(postJsonConverted);
    // print(\'title: ${postModel.title}, description: ${postModel.description}\');

    // print(\'${json.encode(postModel)}\');
  }

  Future<List<Post>> fetchPosts() async {
    final response = 
        await http.get(\'https://resources.ninghao.net/demo/posts.json\');
    
    // print(\'statusCode: ${response.statusCode}\');
    // print(\'body: ${response.body}\');

    if (response.statusCode == 200) {
      final responseBody = json.decode(response.body);
      List<Post> posts = responseBody[\'posts\']
        .map<Post>((item) => Post.fromJson(item))
        .toList();

      return posts;
    } else {
      throw Exception(\'Failed to fetch posts.\');
    }
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: fetchPosts(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        print(\'data: ${snapshot.data}\');
        print(\'connectionState: ${snapshot.connectionState}\');
        
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Center(
            child: Text(\'loading...\'),
          );
        }

        return ListView(
          children: snapshot.data.map<Widget>((item) {
            return ListTile(
              title: Text(item.title),
              subtitle: Text(item.author),
              leading: CircleAvatar(
                backgroundImage: NetworkImage(item.imageUrl),
              ),
            );
          }).toList(),
        );
      },
    );
  }
}

class Post {
  final int id;
  final String title;
  final String description;
  final String author;
  final String imageUrl;

  Post(
    this.id,
    this.title,
    this.description,
    this.author,
    this.imageUrl,
  );

  Post.fromJson(Map json)
    : id = json[\'id\'],
      title = json[\'title\'],
      description = json[\'description\'],
      author = json[\'author\'],
      imageUrl = json[\'imageUrl\'];

  Map toJson() => {
    \'title\': title,
    \'descritpion\': description,
  };
}
 http: ^0.12.0

以上是关于Flutter异步编程 http网络请求数据的主要内容,如果未能解决你的问题,请参考以下文章

Flutter异步获取网络数据

Flutter学习-Dart异步

Flutter学习-网络请求

FLutter入门:异步加载组件FutureBuilder

IOS - 网络(HTTP请求同步请求异步请求JSON解析数据)

Flutter 异步编程