Show menu

main.dart

  void main() => runApp(DemoApp());

  class DemoApp extends StatelessWidget {
    const DemoApp();

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
          home: Scaffold(
              appBar: AppBar(
                title: const Text("Download Demo"),
              ),
              body: Center(
                child: const FetchDataPage(),
              )
          )
      );
    }
  }
  

api/json_models/post.dart

  part 'post.g.dart';

  @JsonSerializable()
  class Post {
    final int userId;
    final int id;
    final String title;
    final String body;
    const Post(this.userId, this.id, this.title, this.body);

    factory Post.fromJson(Map<String, dynamic> json) => _$PostFromJson(json);
    Map<String, dynamic> toJson() => _$PostToJson(this);
  }
  

api/json_models/todos.dart

  part "todo.g.dart";

  @JsonSerializable(explicitToJson: true)
  class TodoObject {
    final List<Todo> data;
    const TodoObject(this.data);

    factory TodoObject.fromJson(Map<String, dynamic> json) =>
        _$TodoObjectFromJson(json);

    Map<String, dynamic> toJson() => _$TodoObjectToJson(this);
  }

  @JsonSerializable()
  class Todo {
    final int userId;
    final int id;
    final String title;
    final bool completed;

    const Todo(this.userId, this.id, this.title, this.completed);

    factory Todo.fromJson(Map<String, dynamic> json) =>
        _$TodoFromJson(json);

    Map<String, dynamic> toJson() => _$TodoToJson(this);
  }
  

api/json_parsers/json_parsers.dart

  library json_parser;

  export 'post_parser.dart';
  export 'todo_parser.dart';
  export 'object_decoder.dart';

  abstract class JsonParser<T> {
    const JsonParser();

    Future<T> parseFromJson(String json);
  }
  

api/json_parsers/object_decoder.dart

  mixin ObjectDecoder<T> on JsonParser<T> {
    Map<String, dynamic> decodeJsonObject(String json) =>
        jsonDecode(json) as Map<String, dynamic>;
  }

  mixin ListDecoder<T> on JsonParser<T> {
    List<dynamic> decodeJsonList(String json) =>
        jsonDecode(json) as List<dynamic>;
  }
  

api/json_parsers/post_parser.dart

  class PostParser extends JsonParser<Post> with ObjectDecoder<Post> {
    const PostParser();

    @override
    Future<Post> parseFromJson(String json) async {
      final decoded = decodeJsonObject(json);
      return Post.fromJson(decoded);
    }
  }
  

api/json_parsers/todo_parser.dart

  class TodoParser extends JsonParser<List<Todo>> with ListDecoder<List<Todo>> {
    const TodoParser();

    @override
    Future<List<Todo>> parseFromJson(String json) async {
      return decodeJsonList(json)
          .map((value) => Todo.fromJson(value as Map<String, dynamic>))
          .toList();
    }
  }
  

api/http_client.dart

  class RequestREST {
    final String endpoint;
    final Map<String, String> data;
    const RequestREST({
      this.endpoint,
      this.data = const {},
    });

    /// HTTP dio client
    static final _client = Dio(
        BaseOptions(
          baseUrl: "https://jsonplaceholder.typicode.com/",
          connectTimeout: 3000, // 3 seconds
          receiveTimeout: 3000, // 3 seconds
          headers: <String, String>{"api-key": "add_one_if_needed", },
        )
    );

    Future<T> executeGet<T>(JsonParser<T> parser) async {
      final response = await _client.get<String>(endpoint);
      return parser.parseFromJson(response.data);
    }

    Future<T> executePost<T>(JsonParser<T> parser) async {
      final formData = FormData.fromMap(data);
      final response = await _client.post<String>(
        endpoint,
        data: formData,
      );

      return parser.parseFromJson(response.data);
    }
  }
  

This website and the book are not official Google products. No affiliations are involved. Built with Java 14 and Vert.X

"Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC"