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

# A packages reduces the complexity of working with using http # response as Models

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: (json) => Todo?.fromJson(json),
      );

      print(allProductsData.map((e) => e?.toJson()).toList());
    } on SimpleError catch (exception) {
      print(exception.message);
    } catch (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: (json) => Todo?.fromJson(json),
        // transformer: (transform) => transform['data'],
      );
      print(productsData?.toJson());
    } on SimpleError catch (exception) {
      print(exception.message);
    } catch (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
140
points
22
downloads

Publisher

unverified uploader

Weekly Downloads

# A packages reduces the complexity of working with using http # response as Models

Homepage
Repository (GitHub)
View/report issues

Topics

#dio #http #network #interceptor #middleware

Documentation

API reference

License

MIT (license)

Dependencies

dio, flutter

More

Packages that depend on simple_fetch