Flutter 中的 PageView.Builder 在 null 上调用了 getter 'length'。接收者:空尝试调用:长度

时间:2021-02-01 13:13:29

标签: flutter viewbuilder

当使用来自 API 的响应时显示错误 getter 'length' 被调用为 null。 接收器:空 尝试调用:长度 这是我的 API 代码

var url =
      "https://domain.php";
  var res;
  var splashs;
  void initState() {
    super.initState();
    fetchData();
  }

  fetchData() async {
    res = await http.get(url);
    splashs = jsonDecode(res.body);
    setState(() {});
  }

使用 List 后代码正常运行

 List<Map<String, String>> splashs = [
    {
      "header": "Flatros",
      "text": "Welcome to Flatros, Let’s shop!",
      "image_name": "assets/images/splash_1.png"
    },
    {
      "header": "Shopping",
      "text":
          "We help people conect with store \naround United State of America",
      "image_name": "assets/images/splash_2.png"
    },
    {
      "header": "Multi Category",
      "text": "FInal Screen",
      "image_name": "assets/images/splash_3.png"
    },
  ];

1 个答案:

答案 0 :(得分:0)

您可能需要将数据转换为 ListArray

  List splashs = new List();
  
  fetchData() async {
    res = await http.get(url);
    final data = jsonDecode(res.body);
    for (var i in data) {
      splashs.add(i);   // Create a list and add data one by one
    }
    setState(() {});
  }