forked from fluttercommunity/get_it
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit de04750
Showing
13 changed files
with
678 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.DS_Store | ||
.dart_tool/ | ||
|
||
.packages | ||
.pub/ | ||
|
||
build/ | ||
ios/.generated/ | ||
ios/Flutter/Generated.xcconfig | ||
ios/Runner/GeneratedPluginRegistrant.* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,13 @@ | ||
{ | ||
// Verwendet IntelliSense zum Ermitteln möglicher Attribute. | ||
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen. | ||
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Flutter", | ||
"request": "launch", | ||
"type": "dart" | ||
} | ||
] | ||
} |
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,3 @@ | ||
## [0.0.1] - TODO: Add release date. | ||
|
||
* TODO: Describe initial release. |
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 @@ | ||
TODO: Add your license here. |
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,9 @@ | ||
# get_it | ||
|
||
A new flutter package project. | ||
|
||
## Getting Started | ||
|
||
For help getting started with Flutter, view our online [documentation](https://flutter.io/). | ||
|
||
For help on editing package code, view the [documentation](https://flutter.io/developing-packages/). |
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,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/lib" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.idea" /> | ||
<excludeFolder url="file://$MODULE_DIR$/.pub" /> | ||
<excludeFolder url="file://$MODULE_DIR$/build" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Dart Packages" level="project" /> | ||
<orderEntry type="library" name="Dart SDK" level="project" /> | ||
<orderEntry type="library" name="Flutter Plugins" level="project" /> | ||
</component> | ||
</module> |
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,69 @@ | ||
library get_it; | ||
|
||
typedef FactoryFunc = Object Function(); | ||
|
||
/// A Calculator. | ||
class GetIt { | ||
static Map<Type, _ServiceFactory> _factories = new Map<Type, _ServiceFactory>(); | ||
|
||
static T get<T>() { | ||
var object = _factories[T]; | ||
if (object == null) { | ||
throw new Exception("Object of type ${T.toString()} is not registered inside GetIt"); | ||
} | ||
return object.getObject() as T; | ||
} | ||
|
||
static void register<T>(FactoryFunc func) { | ||
var factory = new _ServiceFactory(); | ||
factory.create = func; | ||
factory.type = ServiceFactoryType.alwaysNew; | ||
_factories[T] = factory; | ||
} | ||
|
||
static void registerLazySingleton<T>(FactoryFunc func) { | ||
var factory = new _ServiceFactory(); | ||
factory.create = func; | ||
factory.type = ServiceFactoryType.lazy; | ||
_factories[T] = factory; | ||
} | ||
|
||
static void registerConstant<T>(T instance) { | ||
var factory = new _ServiceFactory(); | ||
factory.instance = instance; | ||
factory.type = ServiceFactoryType.constant; | ||
_factories[T] = factory; | ||
} | ||
} | ||
|
||
enum ServiceFactoryType { alwaysNew, constant, lazy } | ||
|
||
class _ServiceFactory { | ||
ServiceFactoryType type; | ||
|
||
FactoryFunc create; | ||
Object instance; | ||
|
||
T getObject<T>() { | ||
try { | ||
switch (type) { | ||
case ServiceFactoryType.alwaysNew: | ||
return create() as T; | ||
break; | ||
case ServiceFactoryType.constant: | ||
return instance as T; | ||
break; | ||
case ServiceFactoryType.lazy: | ||
if (instance == null) { | ||
instance = create(); | ||
} | ||
return instance as T; | ||
break; | ||
} | ||
} catch (e, s) { | ||
print("Error while creating ${T.toString()}"); | ||
print('Stack trace:\n $s'); | ||
rethrow; | ||
} | ||
} | ||
} |
Oops, something went wrong.