-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hive,Flutter local notification, connectivity
- Loading branch information
Moktadir
authored and
Moktadir
committed
Aug 8, 2023
1 parent
bf23471
commit 3380f4d
Showing
27 changed files
with
942 additions
and
501 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'package:getx_standard/app/modules/home-with-restAPI/model/posts.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
|
||
class MyHive { | ||
// Prevent making an instance of this class | ||
MyHive._(); | ||
|
||
// Hive box to store movie data | ||
static late Box<Posts> _postBox; | ||
|
||
// Box name, it's like the table name | ||
static const String _postBoxName = 'posts'; | ||
|
||
/// Initialize local db (HIVE) | ||
/// Pass testPath only if you are testing hive | ||
static Future<void> init( | ||
{Function(HiveInterface)? registerAdapters, String? testPath}) async { | ||
if (testPath != null) { | ||
Hive.init(testPath); | ||
} else { | ||
await Hive.initFlutter(); | ||
} | ||
await registerAdapters?.call(Hive); | ||
await initPostsBox(); | ||
} | ||
|
||
/// Initialize post box | ||
static Future<void> initPostsBox() async { | ||
_postBox = await Hive.openBox<Posts>(_postBoxName); | ||
} | ||
|
||
/// Save all posts to the database | ||
static Future<void> saveAllPosts(List<Posts> posts) async { | ||
try { | ||
await _postBox.clear(); // Clear existing data | ||
await _postBox.addAll(posts); // Add all posts to Hive | ||
} catch (error) { | ||
// Handle error | ||
} | ||
} | ||
|
||
/// Get all posts from Hive | ||
static List<Posts> getAllPosts() { | ||
final posts = _postBox.values.toList(); | ||
return posts | ||
.cast<Posts>(); // Cast the list to the correct type (List<Posts>) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.