颤振从API获取数据,并使用sqflite保存本地数据

颤振从API获取数据,并使用sqflite保存本地数据,api,flutter,dart,sqflite,Api,Flutter,Dart,Sqflite,我遇到了问题,我正在从API获取数据并保存到本地数据库。 因此,作为一名Flatter的新手,我想找到一种方法将数据保存在数据库中,以供离线支持 这就是问题所在 Unhandled Exception: type 'String' is not a subtype of type 'List<dynamic>' in type cast 我认为你应该替换: String employeeToJson(List<Employee> data) =>

我遇到了问题,我正在从API获取数据并保存到本地数据库。 因此,作为一名Flatter的新手,我想找到一种方法将数据保存在数据库中,以供离线支持

这就是问题所在

Unhandled Exception: type 'String' is not a subtype of type 'List<dynamic>' in type cast

我认为你应该替换:

 String employeeToJson(List<Employee> data) =>
        json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
与:

您已经传入了要编码的列表

您不需要List.from和创建List的内部函数。根据文档,列表不能直接编码

class EmployeeApiProvider {
  Future<List<Employee>> getAllEmployees() async {
    var url = "EXAMPLE";
    Response response = await Dio().get(url);
    print(response.data);

    return (response.data as List).map((employee) {
      print('Inserting $employee');
      DBProvider.db.createEmployee(Employee.fromJson(employee));
    }).toList();
  }
}
[
{
id_surat: "1",
nama: "Al Fatihah",
nomor: "1",
arti: "Pembukaan"
},
{
id_surat: "2",
nama: "Al Baqarah",
nomor: "2",
arti: "Sapi Betina"
},
{
id_surat: "3",
nama: "Ali Imran",
nomor: "3",
arti: "Keluarga Imran"
},
{
id_surat: "4",
nama: "An Nisaa",
nomor: "4",
arti: "Wanita"
},
]
 String employeeToJson(List<Employee> data) =>
        json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
String employeeToJson(List<Employee> data) => json.encode(data);