我是飞镖/颤振的新手。我需要获取 Web API 的数据并将其作为 LocalStorage 放入 Hive.box 中。我可以获取一个 API 数据,然后放入 Hive,但我无法将这些数据保存在其他页面中,例如,我无法在其他页面中使用 localStorage 变量......问题是 HiveBox 返回 null。
Main.dart - 打开盒子
Future _abrirCaixa() async {
var dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
return await Hive.openBox('localStorage');
}
void main() {
_abrirCaixa();
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp(),
));
}
Login.dart 中的 RaisedButton
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: ButtonTheme(
height: 40.0,
child: RaisedButton(
onPressed: () async {
if (controladorUsuario.text.isEmpty ||
controladorSenha.text.isEmpty) {
camposVazios.camposVazios(context);
} else {
await fazerLogin(context);
infoAddADM();
infoAddCond();
}
},
// await infoAddCond();
// await infoAddADM();
child: Text(
"Conectar",
style: TextStyle(color: white),
),
color: mainColor,
),
),
),
fazerLogin() => 在 sessionValidation 我把所有数据放在 LocalStorage
[...]
var dados = await Session.sessionValidation(login, senha);
print('------------------------------');
print(localStorage.values.toString());
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => TestePage(),
),
);
[...]
测试页.dart
class _TestePageState extends State<TestePage> {
@override
Widget build(BuildContext context) {
print('--------------//---------------------');
print(localStorage.values.toString());
return Container();
}
}
什么回报给我:
At first:
I/flutter (14463): ------------------------------
I/flutter (14463): (3, Adm ConectCon, 53644, 4, 0, Condomínio SID, 509, 140, ..., null, 90377)
I/flutter (14463): --------------//---------------------
I/flutter (14463): (3, Adm ConectCon, 53644, 4, 0, Condomínio SID, 509, 140, ..., null, 90377)
After a hot reload:
I/flutter (14463): --------------//---------------------
I/flutter (14463): ()