0 purchases
cache data
cache_data #
A Flutter library to fetch data then save the data with duration as a age of data using hive; return dart objects.
This library depend on hive for the database and dart_json_mapper for deserialize JSON to Dart object
Getting started #
Add the dependency to pubspec.yaml:
dependencies:
cache_data:
dart_json_mapper:
dart_json_mapper_flutter:
dev_dependencies:
build_runner:
copied to clipboard
Put @jsonSerializable at your model.
data.dart
@jsonSerializable
class Data {
int userId;
int id;
String title;
Data({
required this.userId,
required this.id,
required this.title,
});
}
copied to clipboard
Next is add main.mapper.g.dart and initializes package.
main.dart
import 'main.mapper.g.dart' show initializeJsonMapper;
import 'package:dart_json_mapper_flutter/dart_json_mapper_flutter.dart' show flutterAdapter;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await CacheData.init();
initializeJsonMapper(adapters: [flutterAdapter]);
runApp(const MyApp());
}
copied to clipboard
Next, create the build.yaml in project directory.
targets:
$default:
builders:
dart_json_mapper:
generate_for:
# here should be listed entry point files having 'void main()' function
- lib/main.dart
# This part is needed to tell original reflectable builder to stay away
# it overrides default options for reflectable builder to an **empty** set of files
reflectable:
generate_for:
- no/files
copied to clipboard
Now generate main.mapper.g.dart files
pub run build_runner build --delete-conflicting-outputs
copied to clipboard
or
pub run build_runner watch --delete-conflicting-outputs
copied to clipboard
if you want to generate files everytime you make changes in the code.
Usage #
final cache = CacheData<List<Data>>();
FutureBuilder<List<Data>?>(
future: cache.fetchData(
'https://jsonplaceholder.typicode.com/albums',
duration: duration),
builder: (context, snapshot) {
...
},
)
copied to clipboard
Additional information #
For more details about how to use dart_json_mapper, you can visit this package
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.