【问题标题】:PageView.Builder in Flutter The getter 'length' was called on null. Receiver: null Tried calling: lengthFlutter 中的 PageView.Builder 在 null 上调用了 getter 'length'。接收方:null 尝试调用:长度
【发布时间】:2021-02-03 20:45:50
【问题描述】:

当使用来自 API 的响应时显示错误 在 null 上调用了 getter 'length'。 接收方:空 尝试调用:长度 这是我的 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"
    },
  ];

【问题讨论】:

  • 尝试使用FutureBuilder 调用,或者您可以显示加载程序,直到数据获取
  • 您可以发布您的 UI 代码,以便我们提供建议
  • @ShubhamNarkhede 我也尝试 ListView.Builder 和 FutureBuilder 但同样的问题
  • @YasirMehmood 请添加您的代码
  • @YasirMehmood 你在这里得到了什么res.body

标签: flutter viewbuilder


【解决方案1】:

您可能需要将数据转换为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(() {});
  }

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多