flutter_data_sync

A Flutter package that simplifies the mapping between Flutter app models and data from external sources (APIs, Databases, etc.). It provides an efficient solution for transforming JSON data from APIs into Dart objects and vice versa, facilitating integration and data management in your Flutter application. With flutter_data_sync, you can easily handle HTTP requests, process responses, and map data, reducing development time and improving application maintainability.

Features

  • Efficient mapping between Flutter app models and external data
  • Transform JSON data from APIs into Dart objects
  • Simplifies integration and data management in Flutter applications

Getting started

To start using the flutter_data_sync package, add it to your pubspec.yaml file:

dependencies:
  flutter_data_sync: ^version

Then, run flutter pub get to install the package.

For detailed information on how to use the package, refer to the documentation.

Usage

Here's a simple example of how to use the flutter_data_sync package:

Initializing DataSync

import 'package:flutter_data_sync/flutter_data_sync.dart';

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

DataSync dataSync = DataSync(baseApiUrl: "http://127.0.0.1:8080/api/");

Retrieve an item list


DataSyncGetWidget<List<Todo>>(
    setIntervallCall: false, // Add periodic calls
    setIntervallCallDuration: const Duration(seconds: 2),
    onSetIntervall: (timer) {},
    promise: () => dataSync.index<Todo>(
            context: context,
            fromJson: Todo.fromJson,
            url: "todos",
        ),
    hasDataWidget: (data) {
        return ListView.builder(
            shrinkWrap: true,
            itemCount: data.length,
            itemBuilder: (BuildContext context, int index) {
            Todo todo = data[index];
            return ListTile(
                leading: validatingID == todo.id
                    ? SizedBox(
                        width: 15,
                        height: 15,
                        child: DataSyncLoading.loadingBallRotate(
                            color: Colors.blue[800]!)) // DataSyncLoading
                    : Checkbox(
                        value: todo.isValid,
                        onChanged: (value) async {
                        setState(() {
                            validatingID = todo.id ?? 0;
                        });
                        todo.isValid = value!;
                        await valideCheckTodo(todo);
                        }),
                title: Text(todo.libelle),
                subtitle: Text(todo.description),
                trailing: Wrap(
                children: [
                    IconButton(
                    onPressed: () async {
                        if (await showAddTodoModal(
                                context: context, todo: todo) !=
                            null) {
                        setState(() {});
                        }
                    },
                    icon: Icon(
                        Icons.edit,
                        color: Colors.green[800],
                    ),
                    ),
                    deletingID == todo.id
                        ? SizedBox(
                            width: 20,
                            height: 20,
                            child: DataSyncLoading.loadingBallRotate(
                                color: Colors.red[800]!))
                        : IconButton(
                            onPressed: () async {
                            setState(() {
                                deletingID = todo.id ?? 0;
                            });
                            await valideDeleteTodo(todo);
                            },
                            icon: Icon(
                            Icons.delete_forever_outlined,
                            color: Colors.red[800],
                            ),
                        ),
                ],
                ),
                onTap: () {
                print('Item $index tapped');
                },
            );
            });
    }),

Send data (POST or PUT)


bool state = await dataSync.post<Todo, bool>(
        context: context,
        fromJson: (map, statusCode) =>
            statusCode == 200 || statusCode == 201 ? true : false,
        url: "todos",
        data: widget.todo.toJSon());
    if (state) {
        setState(() {
        _isloading = false;
        });
        Navigator.pop(context);
    } else {
        setState(() {
        _isloading = false;
        });
    }

USE CUSTOM CALL


dataSync.initCustom();

await dataSync.custom.call<Todo>(context: context,hasData: Todo.fromJson,defaultValue: Todo(libelle: "", description: ""),url: "url",method: HttpMethods.put);

For more examples, check out the /https://pub.dev/packages/flutter_data_sync folder in the package repository.

Additional information

For more information about the flutter_data_sync package, visit the Flutter package repository.

To contribute to the package or report issues, please visit the GitHub repository.


Libraries

flutter_data_sync