am尝试获取数据,然后将其显示在列表视图中..首先,我将获取一个哈希值,然后将其与我保存的哈希值进行比较(如果相同),然后如果没有,我将获取保存的json数据像这样调用api ...
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
File f = File('$path/mypollshash.txt');
if (f.existsSync()) {
print('exists');
String contents = await f.readAsString();
content = contents;
fetchHash();
} else {
print('not exists');
fetch();
}
return f;
}
Future checkfileexist() async {
try {
final file = await _localFile;
String contents = await file.readAsString();
content = contents;
} catch (e) {
//return 'nothing';
}
}
Future<File> writehash(String hash) async {
final file = await _localFile;
return file.writeAsString('$hash', mode: FileMode.write);
}
Future<File> get _localjson async {
final path = await _localPath;
return File('$path/mypolls.json');
}
Future<File> writejson(String json) async {
final file = await _localjson;
return file.writeAsString('$json', mode: FileMode.write);
}
Future readjson() async {
try {
final file = await _localjson;
String contents = await file.readAsString();
content = contents;
setState(() {
polls = pollsFromJson(content);
});
writejson(pollsToJson(polls));
writehash(polls.hash);
print('here');
// return contents;
} catch (e) {
print('there');
print(e);
// If we encounter an error, return 0
//return 'nothing';
}
}
fetch() async {
String data =
await DefaultAssetBundle.of(context).loadString("assets/mypolls.json");
setState(() {
polls = pollsFromJson(data);
});
writejson(pollsToJson(polls));
writehash(polls.hash);
}
fetchHash() async {
String data = await DefaultAssetBundle.of(context)
.loadString("assets/pollshash.json");
print(content);
final pollshash = pollshashFromJson(data);
if (content == pollshash.hash) {
print('take from the saved json');
readjson();
} else {
print('call api');
fetch();
}
}
并且在这里称呼它:
@override
void initState() {
super.initState();
checkfileexist();
}
问题是,一旦启动页面,我将得到红色错误提示屏幕,其长度为null ..因为尚未提取数据..然后将显示列表。.
我如何获取数据字段,然后构建列表,这样我就不会收到错误消息?
自收到这些消息以来,这些功能还会不断重复:
我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 我/颤动(3556):存在 I /颤振(3556): I / flutter(3556):呼叫api 我/颤动(3556):存在 I /颤振(3556):d1f4bd60f52991d100adafa416f48b52 I / flutter(3556):从保存的json中获取 I / flutter(3556):在这里 .....
我怎么只能叫它一次?该页面何时首次启动?
答案 0 :(得分:0)
您需要使用FutureBuilder
https://docs.flutter.io/flutter/widgets/FutureBuilder-class.html 请参阅示例代码,其中“ _calculation”是您的数据来源。
它需要包装在Future中。
答案 1 :(得分:0)
尝试
Widget _myBuild() {
return FutureBuilder(
future: _fetchData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
// Update UI with data
} else if (snapshot.hasError) {
// show error
}
// show progress indicator
CircularProgressIndicator();
});
}