简体   繁体   English

flutter 与 futurebuilder 和 http json 错误

[英]flutter error with futurebuilder and http json

hello i'm working on an app with flutter and i have an error with Futurebuilder i dont understand please help您好,我正在使用 flutter 开发应用程序,但 Futurebuilder 出现错误,我不明白请帮忙

i trying to get title and desc depending of id with a future builder我试图根据 id 与未来的 builder 获取 title 和 desc

this is my json这是我的 json

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

This is my Future这是我的未来

    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');
  }
}

this is my data class这是我的数据 class

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'],
    );
  }
}

this is mt init这是 mt 初始化

Future<Data> futureData;

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

And this is my futureBuilder这是我的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));
                    }
                  })

And i get this error:我得到这个错误:

type 'int' is not a subtype of type 'Iterable' “int”类型不是“Iterable”类型的子类型

It seems like the error was caused by the ID which should be of type String.似乎错误是由应为 String 类型的 ID 引起的。 That solves the issue.这解决了这个问题。

Than I suggest to implement some check in your model in case the values are empty.我建议在您的 model 中进行一些检查,以防这些值为空。

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',
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM