我正在尝试从Firebase实时数据库中读取数据,并保存和键入强制转换数据。
尝试使用以下方法读取数据时,获取此错误FormatException: Invalid radix-10 number (at character 1):
List<RaceModel> racesList = event.snapshot.children
.map((element) => RaceModel.fromDataSnapshot(element))
.toList();这是我的模型(它的一部分):
class RaceModel {
// General
/// Race info ///
// Race name
final String name;
final int id;
// Race class e.g., human
final String raceClass;
// Race subClass e.g., thief
final String subClass;
// Race description
final String description;
final bool isActive;
// Basic Unit info ///
// Basic Unit
final String basicUnitName;
final int basicUnitOff;
final int basicUnitDef;
final int basicUnitCost;
RaceModel(
{required this.name,
required this.id,
required this.raceClass,
required this.subClass,
required this.description,
required this.isActive,
required this.basicUnitName,
required this.basicUnitOff,
required this.basicUnitDef,
required this.basicUnitCost})
factory RaceModel.fromDataSnapshot(DataSnapshot snapshot) {
return RaceModel(
name: snapshot.child('name').value.toString(),
id: int.parse(snapshot.child('id').value.toString()),
raceClass: snapshot.child('raceClass').value.toString(),
subClass: snapshot.child('subClass').value.toString(),
description: snapshot.child('description').value.toString(),
isActive: snapshot.child('isActive').value.toString() == 'true'
? true
: false,
basicUnitName: snapshot.child('basicUnitName').value.toString(),
basicUnitOff:
int.parse(snapshot.child('basicUnitOff').value.toString()),
basicUnitDef:
int.parse(snapshot.child('basicUnitDef').value.toString()),
basicUnitCost:
int.parse(snapshot.child('basicUnitCost').value.toString()),)
}DB的json出口:
{
"races": [
{
"basicUnitCost": 75,
"basicUnitDef": 0,
"basicUnitName": "Thug",
"basicUnitOff": 0,
"description": "Ordinary looking, life is cheap and thieves' dens are two-a-penny in a Brittonian tribe. War is when a Britt really shines, quick wits and even quicker fingers enable them to use skills passed down for generations to stash resources for later use. Up to no good? You bet your life they are!",
"id": 0,
"isActive": true,
"name": "Brittonian",
"raceClass": "Human",
"racialAbilitiesList": "Homes hold 400 citizens, Hideouts hold 100 citizensitizens && Each building destroyed by thievery yields 6000 crowns && Exploring costs 115% of normal crowns && Ready to go: +25% starting money",
"subClass": "Thief",
}
],
"v": 2
}如果有人有更好的方法让我知道,谢谢。
发布于 2022-08-06 19:52:45
用firebase db URI分配数据库
Future<void> restApiExam() async{
var url= 'https://taw3ia-ac6b2.firebaseio.com/Exam.json';
try{
final response = await http.get(url);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
final List<RaceModel> loadRace =[];
{
// Add your remaining code in this try catch }
catch(e){
throw(e);
}
}查查折返
https://stackoverflow.com/questions/73262320
复制相似问题