当使用FutureBuilder快照列出本地数据库中的数据时,返回null。我没有列出我的数据。当我检查连接状态时,它正在等待。当使用debugprint dbHelper.randevuListesiGetir()方法时,我的数据监听控制台会滚动,但不会监听futurebuilder。
class _RandevularState extends State<Randevular> {
DatabaseHelper dbHelper;
List<Randevu> tumRandevular;
@override
void initState() {
// TODO: implement initState
super.initState();
dbHelper=DatabaseHelper();
tumRandevular=List<Randevu>();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future:dbHelper.randevuListesiGetir() ,
builder: (context,AsyncSnapshot<List<Randevu>> snapshot){
if(snapshot.connectionState==ConnectionState.done){
tumRandevular=snapshot.data;
print("data"+snapshot.data.toString());
}else if(snapshot.connectionState==ConnectionState.waiting){
print("waiting");
}else{
print("error");
}
},
);
}
}
答案 0 :(得分:0)
对于您的问题,您需要先检查快照中是否包含数据,然后再尝试对snapshot.data进行任何操作。最后我有几个技巧。
con = file(file.path("my_texts", "text1"), encoding = "macintosh")
on.exit(close(con)) # Always make sure to close connections!
contents = readLines(con)
import 'package:flutter/material.dart';
class _RandevularState extends State<Randevular> {
DatabaseHelper dbHelper;
List<Randevu> tumRandevular;
Future<List<Randevu>> randevuFuture;
@override
void initState() {
super.initState();
dbHelper = DatabaseHelper();
randevuFuture = dbHelper.randevuListesiGetir();
tumRandevular = List<Randevu>();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: randevuFuture,
builder: (context, AsyncSnapshot<List<Randevu>> snapshot) {
if (snapshot.hasData) {
print("Data: ${snapshot.data}");
final randevuList = snapshot.data;
return ListView.builder(
itemCount: randevuList.length,
itemBuilder: (context, index) {
final currentRandevu = randevuList[index];
return ListTile(
title: Text("${currentRandevu.name}"),
);
});
}
// Also check for possible errors
if (snapshot.hasError) {
print("An error occurred");
return Text("An error occurred");
}
// You probably want to show loading indicator before you have data
return CircularProgressIndicator();
},
);
}
}
中进行API调用,而不是将来:部分。由于您在initState
中进行了函数调用,因此future:
将在每个版本中都进行调用。答案 1 :(得分:0)
databasehelper类中的这些方法
Future<List<Map<String,dynamic>>> randevuGetir() async {
var db = await _getDatabase();
var sonuc = await db.query("randevu");
print("sonuc:$sonuc");
return sonuc;
}
Future<List<Randevu>> randevuListesiGetir() async {
var randevuMapListesi = await randevuGetir();
var randevuListesi = List<Randevu>();
for(Map map in randevuMapListesi){
randevuListesi.add(Randevu.fromMap(map));
}
print(randevuListesi);
return randevuListesi;
}
这些编码我的行列页面
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:kuaforapp/models/musteri.dart';
import 'package:kuaforapp/models/randevu.dart';
import 'package:kuaforapp/utils/database_helper.dart';
class Randevular extends StatefulWidget {
@override
_RandevularState createState() => _RandevularState();
}
class _RandevularState extends State<Randevular> {
List<Randevu> tumRAndevular;
DatabaseHelper databaseHelper;
@override
void initState() {
// TODO: implement initState
super.initState();
tumRAndevular = List<Randevu>();
databaseHelper = DatabaseHelper();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: databaseHelper.randevuListesiGetir(),
builder: (context, AsyncSnapshot<List<Randevu>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
tumRAndevular = snapshot.data;
return ListView.builder(
itemCount: tumRAndevular.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(tumRAndevular[index].randevuId.toString()),
);
});
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
);
}
}