从文件获取数据并显示在屏幕上在Flutter中失败。

huangapple go评论63阅读模式
英文:

fetching data from file and to show on screen failing in flutter

问题

我有一个从json文件恢复数据的屏幕....在这里,我想显示所有用于恢复的数据....

List restoreNotes=[];
Future 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) {
//追加数据
List 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();
}


<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
          );
        },
      ),
    );
  }

huangapple
  • 本文由 发表于 2023年7月23日 16:31:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定