Created
April 7, 2022 10:46
-
-
Save pythoneer/589de94261e429f81d08bcb1850e0c2e to your computer and use it in GitHub Desktop.
demo pattern for offline behavior
This file contains hidden or 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
void main() { | |
print("wir machen ein update\n"); | |
doUpdate(); | |
} | |
void doUpdate() async { | |
var currentValue = ""; | |
try { | |
await repository().forEach((value) { | |
print("war haben: $value"); | |
currentValue = value; | |
}); | |
} catch (e) { | |
print("\tfehler beim webservice: $e"); | |
} | |
print("\nwir benutzen: $currentValue"); | |
} | |
Stream<String> repository() async* { | |
//daten von der platte laden dauert ne sekunde | |
yield await disk(); | |
//throw "kein internet"; //uncomment this to simulate "no network" | |
//daten aus dem web laden dauert ne skunde | |
yield await webservice(); | |
//speichere auf die festplatte aber kein weiterer yield | |
await Future.delayed(Duration(milliseconds: 1000)); | |
} | |
Future<String> webservice() async { | |
await Future.delayed(Duration(milliseconds: 1000)); | |
return "Daten vom Webservice"; | |
} | |
Future<String> disk() async { | |
await Future.delayed(Duration(milliseconds: 1000)); | |
return "Daten von der Festplatte"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment