【问题标题】:flutter error with futurebuilder and http jsonfuturebuilder 和 http json 的颤振错误
【发布时间】:2021-06-09 08:17:41
【问题描述】:

你好,我正在开发一个带有颤振的应用程序,我对 Futurebuilder 有一个错误,我不明白 请帮忙

我试图根据 id 与未来的 builder 获取 title 和 desc

这是我的 json

{"title":"15%GANT","desc":"donne 15 % sur les gants"}

这是我的未来

    Future<Data> fetchData(int id, String type) async {
  var queryParameters = {
    'type': type,
    'id': id,
  };
  String url = 'tartapain.bzh';
  var uri = Uri.https(url, '/api/scan/get.php', queryParameters);
  final response = await http.get(uri);

  if (response.statusCode == 200) {
    if (response.body != null) {
      return Data.fromJson(json.decode(response.body));
    }
  } else {
    throw Exception('Failed to load the post');
  }
}

这是我的数据类

class Data {
  final String title;
  final String desc;

  Data({
    this.title,
    this.desc,
  });

  factory Data.fromJson(Map<String, dynamic> json) {
    print(json['desc'] + " " + json['title'] + "\n");
    return Data(
      desc: json['desc'],
      title: json['title'],
    );
  }
}

这是 mt 初始化

Future<Data> futureData;

  @override
  void initState() {
    super.initState();
    futureData = fetchData(widget.id, 'get_info_id');
  }

这是我的futureBuilder

FutureBuilder(
                  future: futureData,
                  initialData: [],
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Center(child: Text("Loading..."));
                    }
                    if (snapshot.hasData) {
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            snapshot.data.title,
                            style: TextStyle(color: Colors.white, fontSize: 18),
                          ),
                          Text(
                            snapshot.data.desc,
                            style: TextStyle(color: Colors.white, fontSize: 12),
                          )
                        ],
                      );
                    } else if (snapshot.hasError) {
                      return Text(
                        '${snapshot.error}',
                        style: TextStyle(color: Colors.white, fontSize: 12),
                      );
                    } else {
                      return CircularProgressIndicator(
                          valueColor:
                              new AlwaysStoppedAnimation<Color>(Colors.black));
                    }
                  })

我得到了这个错误:

type 'int' 不是 type 'Iterable' 的子类型

【问题讨论】:

  • 在您的 fetchdata 函数中,您是否尝试将 id 更改为字符串而不是 int?
  • 我将 id 更改为查询参数中的字符串,感谢它的工作

标签: json flutter http flutter-futurebuilder


【解决方案1】:

似乎错误是由应为字符串类型的 ID 引起的。 这样就解决了问题。

我建议在你的模型中执行一些检查,以防值为空。

class Data {
  final String title;
  final String desc;

  Data({
    this.title,
    this.desc,
  });

  factory Data.fromJson(Map<String, dynamic> json) {
    //print(json['desc'] + " " + json['title'] + "\n");
    return Data(
      desc: (json['desc']!=null)?json['desc']:'no desc',
      title: (json['title']!=null)?json['title']:'no title',
    );
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 2021-12-23
    • 2021-06-30
    相关资源
    最近更新 更多