An open API service indexing awesome lists of open source software.

https://github.com/afnanalmohd/weatherapp

weather app to learn offline and cashing data with using gitstorge.
https://github.com/afnanalmohd/weatherapp

cashing dart flutter getx

Last synced: 2 months ago
JSON representation

weather app to learn offline and cashing data with using gitstorge.

Awesome Lists containing this project

README

        

flutterlogo

# Offline Mood and Caching Data in Weather App.


This guide will take you through the process of storing data fetched from a remote API, ensuring it can be displayed even when there is no active internet connection. Additionally, it will cover synchronizing data once the connection is restored. In this guide, I'll be using Flutter
and using Getx framework for state management.


## Contents
- [Getting Started](#started).
- [Folder Structure](#folder).
- [Add Dependencies](#dependencies).
- [Initialize Get Storage](#initialize).
- [Controller Class](#controller).
- [Display Cached Data in screen](#screen).
- [Technologies](#technologies).
- [Languages](#languages).
- [Demo](#demo).

## πŸš€ Getting Started

Before installing , make sure you have the following prerequisites installed:

- Flutter: See Flutter's installation guide for instructions on how to install Flutter.

Once you have the prerequisites installed, follow these steps to install:

1. Clone the project repository to your local machine.
2. Navigate to the root directory of the project.
3. Run `flutter pub get` to install the required dependencies.
4. Run `flutter run` to launch the app.

## πŸ”Ž Folder Structure

This Project follows the Model-View-Controller (MVC) design pattern. The primary directory structure consists of three main folders: Common, Core, and Features.

```bash
getStorage
β”œβ”€ ios/
β”œβ”€ android/
β”œβ”€ assets/
β”‚ β”œβ”€ images/
β”‚ β”œβ”€ icons/
β”‚ β”œβ”€ lottie/
β”œβ”€ lib/
β”‚ β”œβ”€ common/
β”‚ β”œβ”€ core/
β”‚ β”‚ β”œβ”€ binding/
β”‚ β”‚ β”œβ”€ constant/
β”‚ β”‚ β”œβ”€ localization/
β”‚ β”‚ β”œβ”€ route/
β”‚ β”‚ β”œβ”€ theme/
β”‚ β”œβ”€ feature/
β”‚ β”‚ β”œβ”€ controller/
β”‚ β”‚ β”œβ”€ service/
β”‚ β”‚ β”œβ”€ model/
β”‚ β”‚ β”œβ”€ screen/
β”‚ β”‚ β”œβ”€ widget/
β”‚ β”œβ”€ main.dart
β”œβ”€ .env
β”œβ”€ pubspec.yaml

```
### a. Common:

Common file is directory typically contains code shared throughout the application. Common elements found here include utility classes and custom widgets.

### b. Core

Core file directory generally holds foundational code and essential business logic for the application. This may encompass functionalities like binding, routing, localization, themes, and other critical components that are used throughout the app.

### c. Features

All the features in the app are organized into folders named after each feature. Each of these folders contains related files, including controller, model, service, screen, and widget.


## Step 1: Add Dependencies

Open the ```pubspec.yaml``` file located in the root of your project, and add the following line under the dependencies section.

```
get_storage: ^2.1.1
connectivity_plus: ^5.0.2
```


## Step 2: Initialize Get Storage

To initialize get storage, we need to call ```GetStorage().init``` in the main function.

```
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await GetStorage.init();
runApp(const MyApp());
}
```


## Step 3: Controller Class

Before we continue, I assume you have created a model class, a service class, and a screen class and are fetching data from the API. Now, let’s create a controller class to handle the business logic of the app. I have created three functions to handle caching and synchronizing data as follows:

### a. function refresh

A function refresh to update the UI once the internet connection is restored.
```
refreshData() async {
await syncData();
}
```

### b. function synchronizing data

The main purpose of the ```syncData``` function is to check the internet connection. By using the ```Connectivity Plus``` package.
Initially, the function checks the internet connection. If the connection is off, it displays data from the GetStorage. Otherwise, it fetches and displays data from the ```getAllWeather``` function.

```
syncData() async {
try {
isLoading = true;
update();
var connectivityResult = await Connectivity().checkConnectivity();
if (connectivityResult == ConnectivityResult.none) {
final cachedWeather = getStorage.read('cachedWeather');
if (cachedWeather != null) {
weather = cachedWeather;
update();
}
} else {
await getAllWeather();
}
}
finally {
isLoading = false;
update();
}
}
```

### c. function get data

This function is designed to retrieve data either from a remote server or locally, depending on the internet connectivity status. If the connection is successful, it fetches the data and saves it in the getstorge. In case of an error, it checks the getstorge and displays the stored data.

```
getAllWeather() async {
try {
await service.getAllWeather(
onDone: (value) {
weather = value;
getStorage.write('cachedWeather', weather);
update();
},
onError: (String error) {
final cachedWeather = getStorage.read('cachedWeather');
if (cachedWeather != null) {
weather = cachedWeather;
update();
}
},
);
}
finally {
isLoading = false;
update();
}
}
```

## Step 4: Display Cached Data in screen

Now that we have cached the data in getStorage, we can display it in our app even when offline or when the API is not accessible.
Call the ```syncData```function to check the internet status. In this UI, there are three cases: first, read from getStorage and display it; second, if there is no internet, cache the data; finally, when the internet is available, update the data.

```
WeatherController().syncData();
return GetBuilder(
builder: (controller) {
if (controller.weather == null) {
final cachedWeather = controller.getStorage.read('cachedWeather');
if (cachedWeather != null) {
return CityWidget();
}
else {
return const Text("No internet and no cached data");
}
}
else {
return CityWidget();
}
},
);
}

```

## Note in using simulator

Sometimes, the ```Connectivity Plus``` package may cause issues with CocoaPods. I recommend the following:

1. Navigate to the /ios folder inside your Flutter project.
2. Locate the ```Podfile.lock``` file and uncomment the line ```platform :ios, '12.0' ```(usually in line 2).
3. Run ```flutter clean```.
4. Run ```flutter run```.

## πŸ’Ό Technologies

| library | Usage
| :-------- | :------------------------- |
| Getx | State management , Navigation, Dependency Management |
| Get Storage |Store and Retrieve Data in Memory|
| Connectivity Plus | Discover Network Connectivity |

### Languages
[![English](https://img.shields.io/badge/Language-English-yellow?style=for-the-badge)](README.md)


### Demo

app weather
app weather