Created
June 11, 2023 09:43
-
-
Save kiyoshi-tokunaga/6f62a2ebfc0f45112a045f5bd8f5f5b9 to your computer and use it in GitHub Desktop.
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
void main() async { | |
runApp(const MyApp()); | |
// 初期化後 | |
await LateStaticUtil.initialize(); | |
// 初期化前に呼ぶ、初期化されていたら1になってる | |
var num = await LateStatic().getNumber(); | |
print(num); | |
} | |
class LateStaticUtil { | |
static Future initialize() async { | |
print('初期化し始める'); | |
await LateStatic.initialize(); | |
} | |
} | |
class LateStatic { | |
static bool isInitialized = false; | |
static int number = 0; | |
static Future initialize() async { | |
print('初期化がはじまり'); | |
await Future.delayed(Duration(seconds: 5)); | |
number++; | |
isInitialized = true; | |
print('初期化が終わり'); | |
} | |
Future<int> getNumber() async { | |
await Future.doWhile(() => !isInitialized); | |
print('初期化が終わったのでnumberを返せる'); | |
return number; | |
} | |
} | |
/* | |
Running Gradle task 'assembleDebug'... | |
✓ Built build/app/outputs/flutter-apk/app-debug.apk. | |
Installing build/app/outputs/flutter-apk/app.apk... | |
Debug service listening on ws://127.0.0.1:59977/M224qsv5dF0=/ws | |
Syncing files to device sdk gphone64 arm64... | |
I/flutter (28415): 初期化し始める | |
I/flutter (28415): 初期化がはじまり | |
E/SurfaceSyncer(28415): Failed to find sync for id=0 | |
W/Parcel (28415): Expecting binder but got null! | |
I/flutter (28415): 初期化が終わり | |
I/flutter (28415): 初期化が終わったのでnumberを返せる | |
I/flutter (28415): 1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment