首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据凝固到模型

数据凝固到模型
EN

Stack Overflow用户
提问于 2022-08-06 18:25:44
回答 1查看 27关注 0票数 0

我正在尝试从Firebase实时数据库中读取数据,并保存和键入强制转换数据。

尝试使用以下方法读取数据时,获取此错误FormatException: Invalid radix-10 number (at character 1)

代码语言:javascript
复制
List<RaceModel> racesList = event.snapshot.children
  .map((element) => RaceModel.fromDataSnapshot(element))
  .toList();

这是我的模型(它的一部分):

代码语言:javascript
复制
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出口:

代码语言:javascript
复制
{
  "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
}

如果有人有更好的方法让我知道,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2022-08-06 19:52:45

用firebase db URI分配数据库

代码语言:javascript
复制
  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);
    }
  }

查查折返

How to get List from firebase in Flutter

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73262320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档