Skip to main content

simple_fetch 0.1.0 copy "simple_fetch: ^0.1.0" to clipboard
simple_fetch: ^0.1.0 copied to clipboard

Turn Dio responses into typed models. Works for JSON objects, lists, and nested payloads across current Flutter versions.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:example/models/todo_model.dart';
import 'package:simple_fetch/simple_fetch.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return const SimpleFetchTestApp();
  }
}

class SimpleFetchTestApp extends StatefulWidget {
  const SimpleFetchTestApp({super.key});

  @override
  State<SimpleFetchTestApp> createState() => _SimpleFetchTestAppState();
}

class _SimpleFetchTestAppState extends State<SimpleFetchTestApp> {
  void getDataFromEndpoint() async {
    SimpleFetch simpleFetch = SimpleFetch();

    String apiUrl = 'https://jsonplaceholder.typicode.com/todos/';

    try {
      List<Todo?> allProductsData = await simpleFetch.getList<Todo>(
        url: apiUrl,
        mapper: Todo.fromJson,
      );

      debugPrint('${allProductsData.map((e) => e?.toJson()).toList()}');
    } on SimpleError catch (exception) {
      debugPrint(exception.toString());
    } catch (e) {
      debugPrint('$e');
    }
  }

  void getOneDataItemFromEndpoint() async {
    SimpleFetch simpleFetch = SimpleFetch();

    String apiUrl = 'https://jsonplaceholder.typicode.com/todos/1';

    try {
      Todo? productsData = await simpleFetch.get<Todo>(
        url: apiUrl,
        mapper: Todo.fromJson,
        // transformer: (transform) => transform['data'],
      );
      debugPrint('${productsData?.toJson()}');
    } on SimpleError catch (exception) {
      debugPrint(exception.toString());
    } catch (e) {
      debugPrint('$e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          const SizedBox(height: 120),
          Column(
            children: [
              CupertinoButton(
                color: Theme.of(context).primaryColor,
                child: const Text("Fetch All..."),
                onPressed: () {
                  getDataFromEndpoint();
                },
              ),
              const SizedBox(height: 60),
              CupertinoButton(
                color: Theme.of(context).primaryColor,
                child: const Text("Single Fetch..."),
                onPressed: () {
                  getOneDataItemFromEndpoint();
                },
              ),
            ],
          ),
        ],
      ),
    );
  }
}
4
likes
160
points
85
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Turn Dio responses into typed models. Works for JSON objects, lists, and nested payloads across current Flutter versions.

Repository (GitHub)
View/report issues

Topics

#dio #http #network #interceptor #middleware

License

MIT (license)

Dependencies

dio, flutter

More

Packages that depend on simple_fetch