我正在尝试在我的 Flutter 应用程序中实现 API 数据缓存,以启用应用程序的离线使用。但是,当我尝试返回数据时,它给了我一个 type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'
错误。
我已验证存储在 hive DB 中的数据是我收到的 api 数据。
!!IMPORTANT!! 这个错误发生的方式很奇怪。它仅在执行 FLUTTER RESTART 并导航到此页面时显示此错误。但是,如果我通过连接到互联网然后导航回主页来获取 API 数据,然后关闭我的网络连接并导航回此页面,则不会显示此错误。
对解决此问题的任何帮助将不胜感激。谢谢:)
我在下面附上了我的代码:
API 类
class HTTPService {
Future<List<DriverStandings>> getDrivers(String year) async {
final String urlStr =
'https://ergast.com/api/f1/' + year + '/driverStandings.json';
final Uri url = Uri.parse(urlStr);
try {
final response = await http.get(url);
if (response.statusCode == 200) {
List<dynamic> result = jsonDecode(response.body)["MRData"]
["StandingsTable"]["StandingsLists"][0]["DriverStandings"];
Hive.box('API_DATA').put('drivers ' + year, result);
List<DriverStandings> drivers = result
.map((dynamic driver) => DriverStandings.fromJson(driver))
.toList();
return drivers;
} else {
throw Exception("Failed to load");
}
} catch (SocketExceptionError) {
print('catching');
List<dynamic> cacheResult =
Hive.box('API_DATA').get('drivers ' + year, defaultValue: []);
if (cacheResult.isNotEmpty) {
print('is not empty');
print(cacheResult);
List<DriverStandings> drivers = cacheResult
.map((dynamic driver) => DriverStandings.fromJson(driver))
.toList();
return drivers;
} else {
return [
DriverStandings(
points: 'null',
driver: Driver(
familyName: 'null',
permanentNumber: 'null',
code: 'null',
givenName: 'null'),
wins: 'null')
];
}
}
}
}
模型类
class DriverStandings {
String points;
String wins;
Driver driver;
DriverStandings(
{required this.points, required this.driver, required this.wins});
factory DriverStandings.fromJson(Map<String, dynamic> json) {
return DriverStandings(
points: json['points'],
driver: Driver.fromJson(json['Driver']),
wins: json['wins']);
}
}
class Driver {
String code;
String givenName;
String familyName;
String permanentNumber;
Driver(
{required this.familyName,
required this.permanentNumber,
required this.code,
required this.givenName});
factory Driver.fromJson(Map<String, dynamic> json) {
return Driver(
familyName: json['familyName'],
permanentNumber: json['permanentNumber'],
code: json['code'],
givenName: json['givenName']);
}
}
要展示的未来建筑
FutureBuilder(
future: httpService.getDrivers('current'),
builder: (BuildContext context,
AsyncSnapshot<List<DriverStandings>> snapshot) {
if (snapshot.hasData) {
return ListView.separated(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
if (driver.points == 'null') {
return NoInternetError();
}
return DriverWidget(
givenName: snapshot
.data![index].driver.givenName,
familyName: snapshot
.data![index].driver.familyName,
code: snapshot.data![index].driver.code,
permanentNumber: snapshot.data![index]
.driver.permanentNumber,
points: snapshot.data![index].points,
wins: snapshot.data![index].wins);
},
separatorBuilder:
(BuildContext context, int index) {
return SizedBox(height: 30.0);
},
);
} else if (snapshot.hasError) {
return Text(
snapshot.error.toString(),
style: TextStyle(color: Colors.white),
);
}
return CircularProgressIndicator();
},
)