英文:
fetching data from file and to show on screen failing in flutter
问题
我有一个从json文件恢复数据的屏幕....在这里,我想显示所有用于恢复的数据....
List
Future
String path =
'/storage/emulated/0/Android/data/com.example.noteapp/backup_data2.json';
File? file = File(path);
bool doesFileExists = await file.exists();
if(doesFileExists==true) {
if (await File(path).length() != 0) {
//追加数据
List
for (var i = 0; i < list.length; i++) {
Note note = Note.fromJson(list[i] as Map<String, dynamic>);
restoreNotes.add(note);
}
}
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
fetchNotes();
}
<details>
<summary>英文:</summary>
I have a screen of restoring data from json file....here i want to show all data which are taken back up for restoring....
List<Note> restoreNotes=[];
Future<void> fetchNotes() async {
String path =
'/storage/emulated/0/Android/data/com.example.noteapp/backup_data2.json';
File? file = File(path);
bool doesFileExists = await file.exists();
if(doesFileExists==true) {
if (await File(path).length() != 0) {
//append data
List<dynamic> list = jsonDecode(await file.readAsString());
for (var i = 0; i < list.length; i++) {
Note note = Note.fromJson(list[i] as Map<String, dynamic>);
restoreNotes.add(note);
}
}
}
}
@override
void initState() {
// TODO: implement initState
super.initState();
fetchNotes();
}
its working but i have to press refresh button for set state
later i will user ListView.builder to show this fetched data....
is it possible without using future.builder?
</details>
# 答案1
**得分**: 1
是的,这是可能的!!在数据获取后,您可以通过调用setState手动触发UI更新。
**您需要使用:**
```dart
@override
void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
    fetchNotes().then((_) {
      setState(() {
        // 在获取数据后调用setState以触发UI更新
      });
    });
  });
}
然后,您需要使用ListView.builder来在屏幕上显示数据。
以下是一个示例代码:
@override
Widget build(BuildContext context) {
  // 使用restoreNotes数据的ListView.builder的UI
  return Scaffold(
    // 这里是Scaffold的内容
    body: ListView.builder(
      itemCount: restoreNotes.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text(restoreNotes[index].title),
          // 在此添加其他ListTile内容
        );
      },
    ),
  );
}
请注意,我已经按照您的要求仅提供代码部分的翻译。
英文:
Yes, it is possible!! you can manually trigger a UI update by calling setState after the data is fetched
You need to use:
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      fetchNotes().then((_) {
        setState(() {
            // Call setState after fetching data to trigger the UI update
         });
     });
   }
Then you need a ListView.builder to show that on Screen.
Here is an example code:
@override
  Widget build(BuildContext context) {
    // Your UI with ListView.builder using restoreNotes data
    return Scaffold(
      // Your Scaffold content here
      body: ListView.builder(
        itemCount: restoreNotes.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(restoreNotes[index].title),
            // Other ListTile content here
          );
        },
      ),
    );
  }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论