【问题标题】:type 'String' is not a subtype of type 'int' of 'index' Flutter“String”类型不是“index”Flutter 的“int”类型的子类型
【发布时间】:2020-04-22 01:47:37
【问题描述】:

我正在从 Flutter 中的 JSON 响应创建一个数据表。 目前,我陷入了一个特定的错误,该错误被描述为这篇文章的标题。 我不确定出了什么问题,所以我正在寻求您的帮助。 这是代码:

import 'dart:io';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:json_table/json_table.dart';

class API extends StatefulWidget {
  final File slika;
  final String response;
  API({Key key, this.slika, this.response}) : super(key: key);

  @override
  _APIState createState() => _APIState();
}

class _APIState extends State<API> {
  var user;
  int rowLength = 0;

  @override
  Widget build(BuildContext context) {
    for (int i = 0; i < jsonDecode(widget.response)['rows'].length; i++) {
      if (jsonDecode(widget.response)['rows'][i].length > rowLength) {
        rowLength = jsonDecode(widget.response)['rows'][i].length;
      }
    }
    print(widget.response);
    return Scaffold(
      backgroundColor: Color(0xFF48494D),
      body: Column(
        children: <Widget>[
          Center(
              child: JsonTable(
            jsonDecode(widget.response),
            tableHeaderBuilder: (String header) {
              return Container(
                padding: EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
                decoration: BoxDecoration(
                    border: Border.all(width: 0.5), color: Colors.grey[300]),
                child: Text(
                  header,
                  textAlign: TextAlign.center,
                  style: Theme.of(context).textTheme.display1.copyWith(
                      fontWeight: FontWeight.w700,
                      fontSize: 14.0,
                      color: Colors.black87),
                ),
              );
            },
            tableCellBuilder: (value) {
              return Container(
                padding: EdgeInsets.symmetric(horizontal: 4.0, vertical: 2.0),
                decoration: BoxDecoration(
                    border: Border.all(
                        width: 0.5, color: Colors.grey.withOpacity(0.5))),
                child: Text(
                  value,
                  textAlign: TextAlign.center,
                  style: Theme.of(context)
                      .textTheme
                      .display1
                      .copyWith(fontSize: 14.0, color: Colors.grey[900]),
                ),
              );
            },
          ))
        ],
      ),
    );
  }
}

我为 Flutter 使用了 json_table 小部件库,因此最终结果应如下所示以供参考:

非常感谢您的帮助。

【问题讨论】:

  • 触发此错误的行是什么?
  • "jsonDecode(widget.response),"
  • 为了效率,你可能应该在小部件的构造函数或状态的didUpdateWidget 中执行json.decode。每次都做build(实际上每次构建几次)是不必要的。您的 json 是否解码为列表而不是地图?更新问题以至少显示 json 的开头。
  • 这是 JSON 响应,解码后: [{"Iznos s PDV": "625,00","%PDV": "Iznos PDV","bez PDV": "bez PDV" ,"Cijena": "Iznos","BBK": "Yorabata","JMJ": "KOM","Koli\u010dina": "1,00","Naziv artikla": "BA\u010cVA 30L"," Artikl": "FAN kod","Rbr.": "1"},{"Iznos s PDV": "625,00.","%PDV": "25,00%","bez PDV": " 500,00","Cijena": " ","BBK": "500,00","JMJ": " ","Koli\u010dina": " ","Naziv artikla": " ","Artikl": "9999800","Rbr.": ""}]
  • 在您共享 index 变量的代码上,这是错误的来源,不可见。可以分享一下代码吗?

标签: json string flutter dart


【解决方案1】:

对于遇到相同问题的每个人,解决方案是更改 Header 变量的变量类型。这是因为 Dart 不能自己将整数反转为字符串。 通过改变这个:

tableHeaderBuilder: (String header) {

到这里:

tableHeaderBuilder: (header) {

你解决了问题!

【讨论】:

    猜你喜欢
    • 2020-10-02
    • 2021-05-10
    • 2019-04-24
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2021-09-08
    • 2021-12-15
    相关资源
    最近更新 更多