如何修复FutureBuilder上的'int'类型不是'String'类型的子类型错误?

时间:2019-04-02 07:44:52

标签: dart flutter

我正在从API获取数据并将其存储在缓存中。我也换了模特班。我也打印了缓存表。并在模型类中将String更改为int并创建表查询

var futureBuilder = FutureBuilder(
      future: getWoDescription(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
            return Text('Press button to start.');
          case ConnectionState.active:
          case ConnectionState.waiting:
            return Text('Awaiting result...');
          case ConnectionState.done:
            if (snapshot.hasError)
              return Text('Error: ${snapshot.error}');
            return createListView(context, snapshot);
        //  return Text('Data: ${snapshot.}');
        }
        return null; // unreachable
      },
    );

方法

 Future getWoDescription() async {
        var catLocal = (await HelperWoDescription().display());
        return catLocal;
      }

小部件代码,-检查一些数据,并新建一个列表

Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
    List values = snapshot.data;
    List woDesArray = [];

    for (int i = 0; i < values.length; i++) {
      if (values[i].r == widget.regId && values[i].t == widget.assetId) {
        woDesArray.add(values[i].w);
      }
    }

    return ListView.builder(
        padding: new EdgeInsets.only(top: 8.0, right: 0.0, left: 0.0),
        itemCount: woDesArray.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () => Navigator.push(context,
                MaterialPageRoute(builder: (context) => overview())),
            child: Card(
              child: Column(children: <Widget>[
                Padding(padding: EdgeInsets.all(4.0)),
                Text('${woDesArray[index]}'),
                Padding(padding: EdgeInsets.all(4.0)),
              ]),
            ),
          );
        });
  }

这是我的数据库类,用于创建和插入表格并从表格中获取数据。

    class HelperWoDescription {
      static final HelperWoDescription _instance =  HelperWoDescription.internal();
      factory HelperWoDescription() => _instance;
      static Database _db;

      Future<Database> get db1 async {
        if (_db != null) return _db;
        _db = await initDb();
        return _db;
      }

      HelperWoDescription.internal();

      initDb() async {
        io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
        String path = join(documentsDirectory.path, "WoDescriptionDB.db");
        var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
        return theDb;
      }

      void _onCreate(Database db, int version) async {
        // When creating the db, create the table
        await db.execute(
            "CREATE TABLE WoDescriptionTable(i INTEGER PRIMARY KEY, d STRING, e INTEGER, w STRING, a INTEGER, r INTEGER, t INTEGER, du DOUBLE, s INTEGER, ra INTEGER, cul INTEGER)");
      }

      Future<void> insert(DataWoDescription assetregister) async {
        //final Database db = await db1;
        var db = await db1;
        await db.insert(
          'WoDescriptionTable',
          assetregister.toMap(),
          conflictAlgorithm: ConflictAlgorithm.replace,
        );
      }

      Future<List<DataWoDescription>> display() async {
        var db = await db1;
        final List<Map<String, dynamic>> maps = await db.query('WoDescriptionTable');

        return List.generate(maps.length, (i) {


 return DataWoDescription(
        i: maps[i]['i'],
        d: maps[i]['d'],
        e: maps[i]['e'],
        w: maps[i]['w'],
        a: maps[i]['a'],
        r: maps[i]['r'],
        t: maps[i]['t'],
        du: maps[i]['du'],
        s: maps[i]['s'],
        ra: maps[i]['ra'],
        cul: maps[i]['cul'],
      );
    });
  }

  Future<void> update(DataWoDescription assetregister) async {
    // final db = await database;
    var db = await db1;
    await db.update(
      'WoDescriptionTable',
      assetregister.toMap(),
      where: "i = ?",
      whereArgs: [assetregister.i],
    );
  }
}

从服务器使用此功能存储

storeWoDescription(String url,String token) async {
  final response = await http.get(
    '${url}/WoDescription',
    headers: {'Authorization': 'Bearer ${token}'},);
  final jsonResponse = json.decode(response.body);
  WoDescription model = WoDescription.fromJson(jsonResponse);
  int length = model.data.length;
  for(int i=0; i<length; i++) {
    var data = DataWoDescription(
      i: model.data[i].i,
      d: model.data[i].d,
      e: model.data[i].e,
      w: model.data[i].w,
      a: model.data[i].a,
      r: model.data[i].r,
      t: model.data[i].t,
      du: model.data[i].du,
      s: model.data[i].s,
      ra: model.data[i].ra,
      cul: model.data[i].cul,
    );
    await HelperWoDescription().insert(data);
  }
}

1 个答案:

答案 0 :(得分:0)

如果您使用这样的数据:Text(str)

您可以将其更改为Text('$str')

因此,如果str为整数,则无论如何它将显示为字符串

UPD: 尝试编辑此部分:

return DataWoDescription(
    i: maps[i]['i'],
    d: '${maps[i]['d']}',
    e: maps[i]['e'],
    w: '${maps[i]['w']}',
    a: maps[i]['a'],
    r: maps[i]['r'],
    t: maps[i]['t'],
    du: maps[i]['du'],
    s: maps[i]['s'],
    ra: maps[i]['ra'],
    cul: maps[i]['cul'],
  );