从某种意义上说,以下 Flutter(也使用 Flame 包)代码是否健壮,在“更新”期间是否存在“首选项”可能不可用的风险,并且应该进行进一步的检查和平衡?建议的代码更改以使其健壮?
import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame/flame.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LightComponent extends SpriteComponent {
SharedPreferences prefs;
LightComponent() : super.fromImage(Vector2(404/4,406/4), Flame.images.fromCache('jupiter_404×406.png'),)
Future<void> onLoad() async {
prefs = await SharedPreferences.getInstance();
}
void render(Canvas c) {
super.render(c);
}
void update(double t) {
super.update(t);
bool showWindow = prefs.getBool('showWindow'); // <== IS THIS OK, OR IS THERE Pref == null potential issue to cover???
// etc
}
}