PageView.Builder in Flutter The getter 'length' was called on null. Receiver: null Tried calling: length

77 Views Asked by At

when use response from API then show error The getter 'length' was called on null. Receiver: null Tried calling: length here is my API code

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(() {});
  }

while use List then code working properly

 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

There are 1 best solutions below

0
On

May be you need to convert your data into List or Array

  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(() {});
  }