飞镖Hive:制造多个openbox

b09cbbtk  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(433)

我用Hive来制作简单的积垢,在Hive文档中关于开箱我们可以这样声明 var box = await Hive.openBox<E>('testBox'); 我的问题是,有可能制作多个openbox吗?我想要这样的东西:

  1. Future _openBox() async {
  2. var dir = await getApplicationDocumentsDirectory();
  3. Hive.init(dir.path);
  4. var box_session = await Hive.openBox("box_session");
  5. var box_comment = await Hive.openBox("box_comment");
  6. return await box_session,box_comment;
  7. }

谢谢。

hxzsmxv2

hxzsmxv21#

是的,您可以返回列表
您可以复制粘贴运行下面的完整代码
代码段

  1. List<Box> boxList = [];
  2. Future<List<Box>> _openBox() async {
  3. var dir = await getApplicationDocumentsDirectory();
  4. Hive.init(dir.path);
  5. var box_session = await Hive.openBox("box_session");
  6. var box_comment = await Hive.openBox("box_comment");
  7. boxList.add(box_session);
  8. boxList.add(box_comment);
  9. return boxList;
  10. }

完整代码

  1. import 'package:flutter/material.dart';
  2. import 'package:path_provider/path_provider.dart';
  3. import 'package:hive/hive.dart';
  4. List<Box> boxList = [];
  5. Future<List<Box>> _openBox() async {
  6. var dir = await getApplicationDocumentsDirectory();
  7. Hive.init(dir.path);
  8. var box_session = await Hive.openBox("box_session");
  9. var box_comment = await Hive.openBox("box_comment");
  10. boxList.add(box_session);
  11. boxList.add(box_comment);
  12. return boxList;
  13. }
  14. void main() async {
  15. WidgetsFlutterBinding.ensureInitialized();
  16. await _openBox();
  17. runApp(MyApp());
  18. }
  19. class MyApp extends StatelessWidget {
  20. // This widget is the root of your application.
  21. @override
  22. Widget build(BuildContext context) {
  23. return MaterialApp(
  24. title: 'Flutter Demo',
  25. theme: ThemeData(
  26. // This is the theme of your application.
  27. //
  28. // Try running your application with "flutter run". You'll see the
  29. // application has a blue toolbar. Then, without quitting the app, try
  30. // changing the primarySwatch below to Colors.green and then invoke
  31. // "hot reload" (press "r" in the console where you ran "flutter run",
  32. // or simply save your changes to "hot reload" in a Flutter IDE).
  33. // Notice that the counter didn't reset back to zero; the application
  34. // is not restarted.
  35. primarySwatch: Colors.blue,
  36. ),
  37. home: MyHomePage(title: 'Flutter Demo Home Page'),
  38. );
  39. }
  40. }
  41. class MyHomePage extends StatefulWidget {
  42. MyHomePage({Key key, this.title}) : super(key: key);
  43. // This widget is the home page of your application. It is stateful, meaning
  44. // that it has a State object (defined below) that contains fields that affect
  45. // how it looks.
  46. // This class is the configuration for the state. It holds the values (in this
  47. // case the title) provided by the parent (in this case the App widget) and
  48. // used by the build method of the State. Fields in a Widget subclass are
  49. // always marked "final".
  50. final String title;
  51. @override
  52. _MyHomePageState createState() => _MyHomePageState();
  53. }
  54. class _MyHomePageState extends State<MyHomePage> {
  55. int _counter = 0;
  56. void _incrementCounter() async{
  57. await boxList[0].put('name','hello');
  58. await boxList[1].put('name','world');
  59. var name1 = await boxList[0].get('name');
  60. var name2 = await boxList[1].get('name');
  61. print('name1 ${name1}');
  62. print('name1 ${name2}');
  63. setState(() {
  64. // This call to setState tells the Flutter framework that something has
  65. // changed in this State, which causes it to rerun the build method below
  66. // so that the display can reflect the updated values. If we changed
  67. // _counter without calling setState(), then the build method would not be
  68. // called again, and so nothing would appear to happen.
  69. _counter++;
  70. });
  71. }
  72. @override
  73. Widget build(BuildContext context) {
  74. // This method is rerun every time setState is called, for instance as done
  75. // by the _incrementCounter method above.
  76. //
  77. // The Flutter framework has been optimized to make rerunning build methods
  78. // fast, so that you can just rebuild anything that needs updating rather
  79. // than having to individually change instances of widgets.
  80. return Scaffold(
  81. appBar: AppBar(
  82. // Here we take the value from the MyHomePage object that was created by
  83. // the App.build method, and use it to set our appbar title.
  84. title: Text(widget.title),
  85. ),
  86. body: Center(
  87. // Center is a layout widget. It takes a single child and positions it
  88. // in the middle of the parent.
  89. child: Column(
  90. // Column is also a layout widget. It takes a list of children and
  91. // arranges them vertically. By default, it sizes itself to fit its
  92. // children horizontally, and tries to be as tall as its parent.
  93. //
  94. // Invoke "debug painting" (press "p" in the console, choose the
  95. // "Toggle Debug Paint" action from the Flutter Inspector in Android
  96. // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
  97. // to see the wireframe for each widget.
  98. //
  99. // Column has various properties to control how it sizes itself and
  100. // how it positions its children. Here we use mainAxisAlignment to
  101. // center the children vertically; the main axis here is the vertical
  102. // axis because Columns are vertical (the cross axis would be
  103. // horizontal).
  104. mainAxisAlignment: MainAxisAlignment.center,
  105. children: <Widget>[
  106. Text(
  107. 'You have pushed the button this many times:',
  108. ),
  109. Text(
  110. '$_counter',
  111. style: Theme.of(context).textTheme.display1,
  112. ),
  113. ],
  114. ),
  115. ),
  116. floatingActionButton: FloatingActionButton(
  117. onPressed: _incrementCounter,
  118. tooltip: 'Increment',
  119. child: Icon(Icons.add),
  120. ), // This trailing comma makes auto-formatting nicer for build methods.
  121. );
  122. }
  123. }

输出

  1. I/flutter ( 8023): name1 hello
  2. I/flutter ( 8023): name1 world

演示

展开查看全部

相关问题