我是飛鏢的新手。這可能是一個非常簡單的問題,但是如何在 Dart 專案中讀取 json 檔案。我在互聯網上找不到任何解決方案。我不想用簡單的 Dart 來做這件事。這應該是可能的,或者?
我必須檔案,一個 Dart 檔案和一個 Json 檔案,都在同一個目錄中。Dart 檔案現在應該打開 Json 檔案并將其保存到變數中。在 python 中,解決方案非常簡單,但我在 dart 中找不到方法。
感謝您的幫助!
uj5u.com熱心網友回復:
讀取檔案并轉換為 json 應該非常簡單:
import 'dart:io';
import 'dart:convert';
import 'dart:async';
void main() async {
final file = File('file.json');
final content = await file.readAsString();
final instance = jsonDecode(content);
// ... profit?
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/408821.html
標籤:
下一篇:引數型別“Future<List<Account>>”不能分配給引數型別“Future<List<Account>>?”
