我对这段代码要做的事情我没有解决这个问题的想法main
我需要的是从 mysql 数据库中获取数据并放入 hive.box 数据库
void main() async {
await Hive.initFlutter();
runApp(HomePage());
}
这是我对 API mysql 数据库和 hivebox getAlldata
Box box;
List list = [];
Future openBox() async {
var document = await getApplicationDocumentsDirectory();
Hive.init(document.path);
box = await Hive.openBox<TodoModel>(todoBoxName);
return;
}
Future getAllData() async {
await openBox();
String url = "http://dhoido.com/sttt/tes/viewHive.php";
try{
var response = await http.get(Uri.parse(url));
var _jsonDecode = jsonDecode(response.body);
// setState(() {
// var resBody = json.decode(response.body);
// list = resBody;
// });
await putData(_jsonDecode);
} catch (SocketException) {
print(SocketException);
}
// get data from database
var mymap = box.toMap().values.toList();
if (mymap.isEmpty){
list.add('empty');
}else{
list = mymap;
}
return Future.value(true);
}
这是我放入数据库 putData
Future putData(data) async {
await box.clear();
//insert data
for(var d in data) {
d = {
"title":d.title.cast<int>(),
"detail":d.detail.cast<int>(),
"isCompleted":true,
};
box.add(d);
}
}
这是我的未来建造者列表futurebuilder
child: FutureBuilder(
future: getAllData(),
builder: (context, snapshot){
if (snapshot.hasData){
if (list.contains('empty')){
return Text(
'No Data'
);
}else{
return Column(
children: [
SizedBox(
height: 25,
),
Image(
image: NetworkImage(
'http://dhoido.com/sttt/images/8.jpg',
),
),
Expanded(
child: RefreshIndicator(
onRefresh: updateData,
child: ListView.builder(
itemCount: list.length,
itemBuilder: (ctxt, index){
return ListTile(
title: Text(
"${list[index]['title']}"
),
trailing: Text(
"${list[index]['detail']}"
),
);
}),
)
)
],
);
}
}else {
return CircularProgressIndicator();
}
},
),
这是我的模型 todomodel
import 'package:hive/hive.dart';
part 'todo_model.g.dart';
@HiveType()
class TodoModel extends HiveObject{
@HiveField(0)
final String title;
@HiveField(1)
final String detail;
@HiveField(2)
final bool isCompleted;
TodoModel({this.title, this.detail, this.isCompleted});
}
这是错误 title
I/flutter (13700): Hive instance has already been initialized. Please call Hive.init only once if possible.
I/flutter (13700): NoSuchMethodError: Class '_InternalLinkedHashMap<String, dynamic>' has no instance getter 'title'.
I/flutter (13700): Receiver: _LinkedHashMap len:4
I/flutter (13700): Tried calling: title