Recently when I want to get data from online I used Rest API and fetched the data from it,but the problem is every time it’s need healthy internet connection.And the thing is I can’t access it when I’m offline. So there I thought of using cache concept and save it in local storage.And so that data could be accessed without internet connection.

Libraries

dependencies:
	  flutter:
	    sdk: flutter

	  #Fonts
	  google_fonts: ^1.1.0

	  # The following adds the Cupertino Icons font to your application.
	  # Use with the CupertinoIcons class for iOS style icons.
	  cupertino_icons: ^0.1.3

	  # For os-specific path directory
	  path_provider: ^1.6.7
	  # Hive
	  hive: ^1.4.1+1
	  hive_flutter: ^0.3.0+2
	  #For rest api calls
	  http: ^0.12.1
	  # state management
	  stacked: ^1.4.3
	  # third party usable services
	  stacked_services: ^0.2.2
	  # navigation
	  auto_route:
	  # inversion of control
	  get_it:
	  injectable:

	dev_dependencies:
	  flutter_test:
	    sdk: flutter

	#generators
	  hive_generator: ^0.5.1
	  build_runner: 
	  auto_route_generator:
	  injectable_generator:

In order to cache let’s go by step to step

Step➊ : Create model

import 'package:hive/hive.dart';

	part 'anime.g.dart';

	@HiveType()
	class Anime {
	  @HiveField(0)
	  final String title;
	  @HiveField(1)
	  final String type;
	  @HiveField(2)
	  final int episodes;
	  @HiveField(3)
	  final String status;
	  @HiveField(4)
	  final String startDate;
	  @HiveField(5)
	  final String endDate;
	  @HiveField(6)
	  final String startingSeason;
	  @HiveField(7)
	  final String broadcastTime;
	  @HiveField(8)
	  final String producers;
	  @HiveField(9)
	  final String licenses;
	  @HiveField(10)
	  final String studios;
	  @HiveField(11)
	  final String sources;
	  @HiveField(12)
	  final String genres;
	  @HiveField(13)
	  final String duration;
	  @HiveField(14)
	  final String rating;
	  @HiveField(15)
	  final double score;
	  @HiveField(16)
	  final int scoredby;
	  @HiveField(17)
	  final int members;
	  @HiveField(18)
	  final int favourites;
	  @HiveField(19)
	  final String description;

	  Anime(
	      {this.title,
	      this.type,
	      this.episodes,
	      this.status,
	      this.startDate,
	      this.endDate,
	      this.startingSeason,
	      this.broadcastTime,
	      this.producers,
	      this.licenses,
	      this.studios,
	      this.sources,
	      this.genres,
	      this.duration,
	      this.rating,
	      this.score,
	      this.scoredby,
	      this.members,
	      this.favourites,
	      this.description});
	}

Then after this run a command in teminal for building model_name.g.dart

flutter pub run build_runner build

#cache-control #hiveproject #hive #cache

Flutter cache with hive
24.65 GEEK