【问题标题】:Proper structure with a Flutter App with JSON带有 JSON 的 Flutter App 的正确结构
【发布时间】:2018-07-21 06:32:17
【问题描述】:

我正在设计一个带有 List-List-Detail 视图(列表概述、每个列表、每个列表中所选项目的详细信息)的应用,该视图从静态 JSON 文件中提取所有数据。

我想知道读取、解析和显示 JSON 文件的最佳结构是什么。我应该为每个视图设计模型:所有列表、每个列表和每个项目的概述,并将它们作为单独的文件吗?是否应该在这些模型文件中进行所有资产加载和 JSON 解析?每个视图是否应该只接受 PODO 形式的解析数据?

我一直按照说明 here 进行整体设计,但我坚持使用 JSON 文件的最佳方式,因为它们向小部件设计添加了异步元素。

【问题讨论】:

  • 请先尝试一下。您的问题可以解释为基于意见

标签: android json flutter


【解决方案1】:

是的,您需要创建模型类来处理 JSON 响应,

您可以使用相同的模型类来列出列表,但不要忘记将子列表项添加到相同的模型中

这里是sample example

import 'dart:async';
import 'dart:convert';

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

Future<List<Photo>> fetchPhotos(http.Client client) async {
  final response =
      await client.get('https://jsonplaceholder.typicode.com/photos');

  // Use the compute function to run parsePhotos in a separate isolate
  return compute(parsePhotos, response.body);
}

// A function that will convert a response body into a List<Photo>
List<Photo> parsePhotos(String responseBody) {
  final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();

  return parsed.map<Photo>((json) => new Photo.fromJson(json)).toList();
}

class Photo {
  final int albumId;
  final int id;
  final String title;
  final String url;
  final String thumbnailUrl;

  Photo({this.albumId, this.id, this.title, this.url, this.thumbnailUrl});

  factory Photo.fromJson(Map<String, dynamic> json) {
    return new Photo(
      albumId: json['albumId'] as int,
      id: json['id'] as int,
      title: json['title'] as String,
      url: json['url'] as String,
      thumbnailUrl: json['thumbnailUrl'] as String,
    );
  }
}

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appTitle = 'Isolate Demo';

    return new MaterialApp(
      title: appTitle,
      home: new MyHomePage(title: appTitle),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  MyHomePage({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(title),
      ),
      body: new FutureBuilder<List<Photo>>(
        future: fetchPhotos(new http.Client()),
        builder: (context, snapshot) {
          if (snapshot.hasError) print(snapshot.error);

          return snapshot.hasData
              ? new PhotosList(photos: snapshot.data)
              : new Center(child: new CircularProgressIndicator());
        },
      ),
    );
  }
}

class PhotosList extends StatelessWidget {
  final List<Photo> photos;

  PhotosList({Key key, this.photos}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new GridView.builder(
      gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
      ),
      itemCount: photos.length,
      itemBuilder: (context, index) {
        return new Image.network(photos[index].thumbnailUrl);
      },
    );
  }
}

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 2021-11-22
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多