Last active
November 9, 2022 05:29
-
-
Save mono0926/46144b9d5371ce234e0b66b3fc77405e 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
import 'dart:async'; | |
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:google_mobile_ads/google_mobile_ads.dart'; | |
import 'package:hooks_riverpod/hooks_riverpod.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
final sharedPreferencesProvider = Provider<SharedPreferences>( | |
(ref) => throw UnimplementedError(), | |
); | |
Future<void> main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
late final SharedPreferences sharedPreferences; | |
await Future.wait( | |
[ | |
Future(() async { | |
sharedPreferences = await SharedPreferences.getInstance(); | |
}), | |
// 順番関係なく並行して実行して良い非同期処理群書いたり、 | |
// SharedPreferencesのようにインスタンス取得が非同期のものを同様に処理したり | |
// (単純にawaitの直列で書くと、処理時間がその合計になって無駄な一方、 | |
// Future.waitで並行処理にしておくと概ねその中の最長のものの処理時間で済む) | |
Firebase.initializeApp(), | |
MobileAds.instance.initialize(), | |
], | |
); | |
runApp( | |
ProviderScope( | |
overrides: [ | |
sharedPreferencesProvider.overrideWithValue(sharedPreferences), | |
], | |
child: Consumer( | |
builder: (context, ref, child) { | |
// 上記のように起動時に取得を済ませてRiverpodで取れるようにしておくと、 | |
// それ以降同期的に取得できて便利 | |
print(ref.watch(sharedPreferencesProvider).get('test')); | |
return child!; | |
}, | |
child: const MaterialApp( | |
home: Scaffold( | |
body: Text('hello'), | |
), | |
), | |
), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment